none
如何把电脑加入域(c#) RRS feed

答案

  • 你好,

    可以尝试一下使用WMI来进行这样的操作,以下是示例代码,可以尝试一下:

    /// <summary>
    /// Join domain and set Machine Name
    /// </summary>
    public static bool JoinAndSetName(string newName)
    {
      _lh.Log(LogHandler.LogType.Debug, string.Format("Joining domain and changing Machine Name from '{0}' to '{1}'...", Environment.MachineName, newName));
    
      // Get WMI object for this machine
      using (ManagementObject wmiObject = new ManagementObject(new ManagementPath("Win32_ComputerSystem.Name='" + Environment.MachineName + "'")))
      {
        try
        {
          // Obtain in-parameters for the method
          ManagementBaseObject inParams = wmiObject.GetMethodParameters("JoinDomainOrWorkgroup");
          inParams["Name"] = "domain_name";
          inParams["Password"] = "domain_account_password";
          inParams["UserName"] = "domain_account";
          inParams["FJoinOptions"] = 3; // Magic number: 3 = join to domain and create computer account
    
          _lh.Log(LogHandler.LogType.Debug, string.Format("Joining machine to domain under name '{0}'...", inParams["Name"]));
    
          // Execute the method and obtain the return values.
          ManagementBaseObject joinParams = wmiObject.InvokeMethod("JoinDomainOrWorkgroup", inParams, null);
    
          _lh.Log(LogHandler.LogType.Debug, string.Format("JoinDomainOrWorkgroup return code: '{0}'", joinParams["ReturnValue"]));
    
          // Did it work?
          if ((uint)(joinParams.Properties["ReturnValue"].Value) != 0)
          {
            // Join to domain didn't work
            _lh.Log(LogHandler.LogType.Fatal, string.Format("JoinDomainOrWorkgroup failed with return code: '{0}'", joinParams["ReturnValue"]));
            return false;
          }
        }
        catch (ManagementException e)
        {
          // Join to domain didn't work
          _lh.Log(LogHandler.LogType.Fatal, string.Format("Unable to join domain '{0}'", _targetDomain), e);
          return false;
        }
    
        // Join to domain worked - now change name
        ManagementBaseObject inputArgs = wmiObject.GetMethodParameters("Rename");
        inputArgs["Name"] = newName;
        inputArgs["Password"] = "domain_account_password";
        inputArgs["UserName"] = "domain_account";
    
        // Set the name
        ManagementBaseObject nameParams = wmiObject.InvokeMethod("Rename", inputArgs, null);
        _lh.Log(LogHandler.LogType.Debug, string.Format("Machine Rename return code: '{0}'", nameParams["ReturnValue"]));
    
        if ((uint)(nameParams.Properties["ReturnValue"].Value) != 0)
        {
          // Name change didn't work
          _lh.Log(LogHandler.LogType.Fatal, string.Format("Unable to change Machine Name from '{0}' to '{1}'", Environment.MachineName, newName));
          return false;
        }
    
        // All ok
        return true;
      }
    }

    关于

    如何使用WMI,请参考这篇博客:

    http://www.cnblogs.com/ceachy/archive/2013/03/21/2973659.html


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月27日 3:36
    版主