简体   繁体   English

在C#中重启一次重命名计算机并加入域

[英]Rename computer and join domain with one reboot in C#

I'm trying to make a program that makes it easier for us to deploy new computers on the domain. 我正在尝试制作一个程序,使我们更容易在域上部署新计算机。 What I want it to do, is simply rename the computer, and join it to our domain (it's going to be doing a lot of other stuff, but this isn't coming in till I've got this working). 我想要它做的只是重命名计算机,并将它加入我们的域(它将会做很多其他的东西,但是直到我有这个工作才会进入)。

Code here: http://pastebin.com/ayREYH0C 代码在这里: http//pastebin.com/ayREYH0C

Stolen from http://www.experts-exchange.com/Programming/Languages/.NET/Q_26262588.html and then changed to fit my needs http://www.experts-exchange.com/Programming/Languages/.NET/Q_26262588.html窃取然后更改以满足我的需求

My problem is that the domain join doesn't respect the rename of the computer. 我的问题是域加入不尊重计算机的重命名。 I've put in a Thread.Sleep between the rename and the join so I had some time to check if the rename happens at all, and it does! 我在重命名和连接之间放了一个Thread.Sleep,所以我有时间检查重命名是否发生了,确实如此! When the domainjoin happens, it reverts back to the old name though and that is the computername that gets created in the domain, not the new name. 当域连接发生时,它会恢复为旧名称,这是在域中创建的计算机名,而不是新名称。

I've searched all around, but not found an issue like this, nor a solution for this particular need. 我四处搜索,但没有找到这样的问题,也没有解决这个特殊需求。

Here is ac# function that joins a workstation to a domain...thought you might find use of it as an alternate method that avoids WMI and uses ManagementObject instead: 这是将工作站连接到域的ac#函数...以为您可能会将其用作避免WMI并使用ManagementObject的替代方法:

(I realize that the executing credentials still need the rights to rename) (我意识到执行凭证仍然需要重命名的权限)

public static bool Join(string dom,string usr, string pass)
    {
        // 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;

        // Define parameters that we will use later when we invoke the method.
        // Remember, the username must have permission to join the object in the AD.
        //string domain = "domain";
        //string password = "password";
        //string username = "username";
        //string destinationOU = "destinationOU";

        // 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 "
        // For more information see: http://msdn.microsoft.com/en-us/library/aa392154%28VS.85%29.aspx
        int parameters = JOIN_DOMAIN | DOMAIN_JOIN_IF_JOINED;

        // The arguments are passed as an array of string objects in a specific order
        object[] methodArgs = { dom, pass, usr + "@" + dom, null, 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!");
            return true;
        }
        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.ToString());
            return false;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式重命名计算机c#.net - rename computer programmatically c# .net 重新启动计算机后c#加载程序设置 - c# load program settings after reboot computer 通过WCF和IIS c#上托管的WMI重新启动计算机 - Reboot computer through WCF with WMI hosted on IIS c# C#:获取有关域中计算机的信息 - C#: get information about computer in domain 如何使用 C# 在其中运行带有重命名计算机的 Powershell 脚本 - How to run a Powershell script with Rename-Computer in it using C# C#调用Win32 API function 如何取消自动关机/重启/锁定电脑 - How to cancel auto power off/reboot/lock computer by calling Win32 API function in C# 使用C#和Powershell从不属于域的计算机访问Active Directory - Accessing Active Directory From a Computer Not Part of the Domain With C# and Powershell c#Active Directory身份验证用户(如果计算机不在域中) - c# Active Directory Authentication User if Computer not in domain 如何从域中删除计算机(C#代码),然后添加到工作组? - How to remove computer from Domain (C# Code) and then add to workgroup? 如何从 c# 中的完整计算机名称中获取一个简单的计算机名称(没有域名)? - How to get a simple computer name (without the domain name) out of a full computer name in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM