计算机加入域C#程序
之前一直用vbs脚本加域,最近有需求要用c#做一个加入域的客户端,找到两种加域的方法,都可以实现计算机加入域功能。
1、调用WMI对象加域;
public static int SetDomainMembership(string DomainName, string UserName, string Password,out string err)
{
err = "System Error!";
// Invoke WMI to join the domain
using (ManagementObject wmiObject = new ManagementObject(new ManagementPath("Win32_ComputerSystem.Name='" + System.Environment.MachineName + "'")))
{
try
{
// Obtain in-parameters for the method
ManagementBaseObject inParams = wmiObject.GetMethodParameters("JoinDomainOrWorkgroup");
inParams["Name"] = DomainName;
inParams["Password"] = Password;
inParams["UserName"] = UserName + "@" + DomainName;
inParams["AccountOU"] = null;
inParams["FJoinOptions"] = 3; //
// Execute the method and obtain the return values.
ManagementBaseObject outParams = wmiObject.InvokeMethod("JoinDomainOrWorkgroup", inParams, null);
switch (outParams["ReturnValue"].ToString())
{
case "5":
err = "Access is denied";
break;
case "87":
err = "The parameter is incorrect";
break;
case "110":
err = "The system cannot open the specified object";
break;
case "1323":
err = "Unable to update the password";
break;
case "1326":
err = "Logon failure: unknown username or bad password";
break;
case "1355":
err = "The specified domain either does not exist or could not be contacted";
break;
case "2224":
err = "The account already exists";
break;
case "2691":
err = "The machine is already joined to the domain";
break;
case "2692":
err = "The machine is not currently joined to a domain";
break;
}
return Convert.ToInt32(outParams["ReturnValue"]);
}
catch (ManagementException e)
{
// It didn't work
return -1;
}
}
}
err = "System Error!";
// Invoke WMI to join the domain
using (ManagementObject wmiObject = new ManagementObject(new ManagementPath("Win32_ComputerSystem.Name='" + System.Environment.MachineName + "'")))
{
try
{
// Obtain in-parameters for the method
ManagementBaseObject inParams = wmiObject.GetMethodParameters("JoinDomainOrWorkgroup");
inParams["Name"] = DomainName;
inParams["Password"] = Password;
inParams["UserName"] = UserName + "@" + DomainName;
inParams["AccountOU"] = null;
inParams["FJoinOptions"] = 3; //
// Execute the method and obtain the return values.
ManagementBaseObject outParams = wmiObject.InvokeMethod("JoinDomainOrWorkgroup", inParams, null);
switch (outParams["ReturnValue"].ToString())
{
case "5":
err = "Access is denied";
break;
case "87":
err = "The parameter is incorrect";
break;
case "110":
err = "The system cannot open the specified object";
break;
case "1323":
err = "Unable to update the password";
break;
case "1326":
err = "Logon failure: unknown username or bad password";
break;
case "1355":
err = "The specified domain either does not exist or could not be contacted";
break;
case "2224":
err = "The account already exists";
break;
case "2691":
err = "The machine is already joined to the domain";
break;
case "2692":
err = "The machine is not currently joined to a domain";
break;
}
return Convert.ToInt32(outParams["ReturnValue"]);
}
catch (ManagementException e)
{
// It didn't work
return -1;
}
}
}
2、调用NetDom对象加域;
public static int SetDomainMembership2(string DomainName, string UserName, string Password, out string err)
{
err = string.Empty;
try
{
string DomainNameHost = DomainName;
uint value1 = NetJoinDomain(null, DomainNameHost, null, UserName+"@"+DomainName, Password, (JoinOptions.NETSETUP_JOIN_DOMAIN | JoinOptions.NETSETUP_DOMAIN_JOIN_IF_JOINED | JoinOptions.NETSETUP_ACCT_CREATE));
err = value1.ToString();
return Convert.ToInt32(value1) ;
}
catch (Exception e)
{
err = e.ToString();
return -1;
}
}
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
static extern uint NetJoinDomain(
string lpServer,
string lpDomain,
string lpAccountOU,
string lpAccount,
string lpPassword,
JoinOptions NameType);
[Flags]
enum JoinOptions
{
NETSETUP_JOIN_DOMAIN = 0x00000001,
NETSETUP_ACCT_CREATE = 0x00000002,
NETSETUP_ACCT_DELETE = 0x00000004,
NETSETUP_WIN9X_UPGRADE = 0x00000010,
NETSETUP_DOMAIN_JOIN_IF_JOINED = 0x00000020,
NETSETUP_JOIN_UNSECURE = 0x00000040,
NETSETUP_MACHINE_PWD_PASSED = 0x00000080,
NETSETUP_DEFER_SPN_SET = 0x10000000
}
err = string.Empty;
try
{
string DomainNameHost = DomainName;
uint value1 = NetJoinDomain(null, DomainNameHost, null, UserName+"@"+DomainName, Password, (JoinOptions.NETSETUP_JOIN_DOMAIN | JoinOptions.NETSETUP_DOMAIN_JOIN_IF_JOINED | JoinOptions.NETSETUP_ACCT_CREATE));
err = value1.ToString();
return Convert.ToInt32(value1) ;
}
catch (Exception e)
{
err = e.ToString();
return -1;
}
}
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
static extern uint NetJoinDomain(
string lpServer,
string lpDomain,
string lpAccountOU,
string lpAccount,
string lpPassword,
JoinOptions NameType);
[Flags]
enum JoinOptions
{
NETSETUP_JOIN_DOMAIN = 0x00000001,
NETSETUP_ACCT_CREATE = 0x00000002,
NETSETUP_ACCT_DELETE = 0x00000004,
NETSETUP_WIN9X_UPGRADE = 0x00000010,
NETSETUP_DOMAIN_JOIN_IF_JOINED = 0x00000020,
NETSETUP_JOIN_UNSECURE = 0x00000040,
NETSETUP_MACHINE_PWD_PASSED = 0x00000080,
NETSETUP_DEFER_SPN_SET = 0x10000000
}
posted on 2011-11-13 22:54 alronzhang 阅读(2602) 评论(0) 编辑 收藏 举报