2012-04-30 27 views
6

Tôi đang cố gắng tạo một chương trình giúp chúng tôi triển khai các máy tính mới trên miền dễ dàng hơn. Những gì tôi muốn nó làm, chỉ đơn giản là đổi tên máy tính, và tham gia nó vào miền của chúng tôi (nó sẽ được làm rất nhiều thứ khác, nhưng điều này không đến trong cho đến khi tôi đã làm việc này).Đổi tên máy tính và gia nhập miền bằng một lần khởi động lại trong C#

Mã ở đây: http://pastebin.com/ayREYH0C

bị đánh cắp từ http://www.experts-exchange.com/Programming/Languages/.NET/Q_26262588.html và sau đó thay đổi để phù hợp với nhu cầu của tôi

Vấn đề của tôi là tên miền tham gia không tôn trọng việc đổi tên của máy tính. Tôi đã đặt trong một Thread.Sleep giữa đổi tên và tham gia vì vậy tôi đã có một thời gian để kiểm tra xem việc đổi tên xảy ra ở tất cả, và nó! Khi domainjoin xảy ra, nó sẽ trở lại tên cũ và đó là tên máy tính được tạo trong miền, chứ không phải tên mới.

Tôi đã tìm kiếm tất cả xung quanh, nhưng không tìm thấy vấn đề như thế này, cũng không phải là giải pháp cho nhu cầu cụ thể này.

+0

bạn có thấy: http://stackoverflow.com/questions/6217799/rename-computer-and-join-to-domain-in-one-step-with-powershell và http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/ad11d4c0-20cd-406b-94a4-9551cdc73388/ – gordatron

+0

cũng điều này có thể được sử dụng: http://stackoverflow.com/questions/ 4183759/programma tically-join-windows-machine-to-ad-domain – gordatron

+0

Vì vậy, câu trả lời là: Tham gia miền, THEN thay đổi tên máy tính. Điều này trình bày một chút vấn đề, vì không phải quản trị viên không có quyền thay đổi tên người dùng, nhưng tôi sẽ làm việc xung quanh đó, cảm ơn rất nhiều vì sự giúp đỡ :) –

Trả lời

2

Đây là aC# hàm gia nhập một máy trạm vào một miền ... nghĩ bạn có thể tìm thấy ứng dụng của nó như là một phương pháp thay thế mà tránh WMI và sử dụng ManagementObject thay vì:

(Tôi nhận ra rằng các chứng chỉ thực hiện vẫn cần quyền để đổi tên)

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; 
     } 

    } 
Các vấn đề liên quan