gpt4 book ai didi

c# - 使用 "JoinDomainOrWorkgroup"加入域时使用新的 PC 名称

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:42 27 4
gpt4 key购买 nike

我一直在重写我公司的域加入脚本,我正在使用 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 名称,或者在不引用当前机器名称的情况下加入域?

非常感谢!

最佳答案

我终于设法让它工作了;我需要重组我的代码以先将计算机添加到域中,然后更改计算机的名称。

在这里粘贴我的代码以帮助可能有同样问题的其他人:

public static bool JoinAndSetName(string newName, string target, string username, string password)
{
// Get WMI object for this machine
using (ManagementObject computerSystem = new ManagementObject("Win32_ComputerSystem.Name='" + Environment.MachineName + "'"))
{
try
{
object[] methodArgs = { "MyDomain.com", password, username, target, 3 };
computerSystem.Scope.Options.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;
computerSystem.Scope.Options.Impersonation = ImpersonationLevel.Impersonate;
computerSystem.Scope.Options.EnablePrivileges = true;
object joinParams = computerSystem.InvokeMethod("JoinDomainOrWorkgroup", methodArgs);
}
catch (ManagementException e)
{
MessageBox.Show("Join to domain didn't work");
return false;
}

// Join to domain worked - now change name
ManagementBaseObject inputArgs = computerSystem.GetMethodParameters("Rename");
inputArgs["Name"] = newName;
inputArgs["Password"] = password;
inputArgs["UserName"] = username;

// Set the name
ManagementBaseObject nameParams = computerSystem.InvokeMethod("Rename", inputArgs, null);

if ((uint)(nameParams.Properties["ReturnValue"].Value) != 0)
{
MessageBox.Show("Name change didn't work");
return false;
}

// All ok
return true;
}
}

绝对要信用This post ;我修改了它以包括指定 OU 路径和添加身份验证隐私。

关于c# - 使用 "JoinDomainOrWorkgroup"加入域时使用新的 PC 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57009143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com