通过“ JoinDomainOrWorkgroup”加入域时使用新的PC名称

时间:2019-07-12 14:34:39

标签: c#

我一直在重写公司的域加入脚本,并且正在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名称与运行此代码之间重新启动的情况下,无法弄清楚该怎么做。 / p>

我们一直在使用以下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名称或在不引用当前计算机名称的情况下加入域有任何想法?

一吨!

1 个答案:

答案 0 :(得分:0)

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

在此处粘贴我的代码以帮助可能遇到相同问题的其他人:

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路径并添加身份验证隐私。