【发布时间】:2019-07-12 14:34:39
【问题描述】:
我一直在重写我公司的域加入脚本,我正在使用 C# 中的“JoinDomainOrWorkgroup”方法将计算机加入域:
void Join(string newPCName, string destinationOU)
{
// Define constants used in the method.
int JOIN_DOMAIN = 1;
int ACCT_CREATE = 2;
int ACCT_DELETE = 4;
int WIN9X_UPGRADE = 16;
int DOMAIN_JOIN_IF_JOINED = 32;
int JOIN_UNSECURE = 64;
int MACHINE_PASSWORD_PASSED = 128;
int DEFERRED_SPN_SET = 256;
int INSTALL_INVOCATION = 262144;
string domain = "MyDomain.com";
string password = passwordBox.Text;
string username = usernameBox.Text;
// Here we will set the parameters that we like using the logical OR operator.
// If you want to create the account if it doesn't exist you should add " | ACCT_CREATE "
int parameters = JOIN_DOMAIN | ACCT_CREATE;
// The arguments are passed as an array of string objects in a specific order
object[] methodArgs = { domain, password, username, destinationOU, parameters };
// Here we construct the ManagementObject and set Options
ManagementObject computerSystem = new ManagementObject("Win32_ComputerSystem.Name='" + Environment.MachineName + "'");
computerSystem.Scope.Options.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;
computerSystem.Scope.Options.Impersonation = ImpersonationLevel.Impersonate;
computerSystem.Scope.Options.EnablePrivileges = true;
// Here we invoke the method JoinDomainOrWorkgroup passing the parameters as the second parameter
object Oresult = computerSystem.InvokeMethod("JoinDomainOrWorkgroup", methodArgs);
// The result is returned as an object of type int, so we need to cast.
int result = (int)Convert.ToInt32(Oresult);
// If the result is 0 then the computer is joined.
if (result == 0)
{
MessageBox.Show("Joined Successfully!");
this.Close();
return;
}
else
{
// Here are the list of possible errors
string strErrorDescription = " ";
switch (result)
{
case 5:
strErrorDescription = "Access is denied";
break;
case 87:
strErrorDescription = "The parameter is incorrect";
break;
case 110:
strErrorDescription = "The system cannot open the specified object";
break;
case 1323:
strErrorDescription = "Unable to update the password";
break;
case 1326:
strErrorDescription = "Logon failure: unknown username or bad password";
break;
case 1355:
strErrorDescription = "The specified domain either does not exist or could not be contacted";
break;
case 2224:
strErrorDescription = "The account already exists";
break;
case 2691:
strErrorDescription = "The machine is already joined to the domain";
break;
case 2692:
strErrorDescription = "The machine is not currently joined to a domain";
break;
}
MessageBox.Show(strErrorDescription);
return;
}
}
效果很好!唯一的问题是我需要它在加入域时使用新名称,而不是当前机器名称,并且无法弄清楚如何在以编程方式更改 PC 名称和运行此代码之间不重新启动的情况下执行此操作。
我们一直在使用以下 PowerShell 命令加入域,这允许我们使用新名称加入域,然后重新启动并在一次重启中设置新名称:
Add-Computer -NewName $ComputerName.ToUpper() -DomainName "MyDomain.com" -Credential $cred -OUPath $Target -ErrorAction Continue
有没有办法在 C# 中实现这一点?我试过换行:
ManagementObject computerSystem = new ManagementObject("Win32_ComputerSystem.Name='" + Environment.MachineName + "'");
到:
ManagementObject computerSystem = new ManagementObject("Win32_ComputerSystem.Name='" + newPCName + "'");
但如果“newPCName”与当前 PC 名称不匹配,它只会引发错误。
是否有人对如何引用“待定”PC 名称或在不引用当前机器名称的情况下加入域有任何想法?
非常感谢!
【问题讨论】:
-
您是否先尝试重命名然后加入? docs.microsoft.com/en-us/windows/win32/cimwin32prov/…
-
刚想通,需要先加入再改名。不过谢谢!
标签: c#