2012-04-30 10 views
6

में एक रीबूट के साथ डोमेन में शामिल हों, मैं एक ऐसा प्रोग्राम बनाने की कोशिश कर रहा हूं जो हमारे लिए डोमेन पर नए कंप्यूटर को तैनात करना आसान बनाता है। मैं इसे क्या करना चाहता हूं, बस कंप्यूटर का नाम बदलें, और इसे हमारे डोमेन में शामिल करें (यह कई अन्य सामान करने जा रहा है, लेकिन यह तब तक नहीं आ रहा है जब तक कि यह काम नहीं कर रहा हो)।कंप्यूटर का नाम बदलें और सी #

यहाँ कोड: http://pastebin.com/ayREYH0C

http://www.experts-exchange.com/Programming/Languages/.NET/Q_26262588.html से चोरी और फिर मेरी जरूरतों

मेरे समस्या फिट करने के लिए बदल गया है कि डोमेन में शामिल होने के लिए कंप्यूटर का नाम बदलने सम्मान नहीं करता है। मैंने थ्रेड में डाल दिया है। नाम बदलने और शामिल होने के बीच सो जाओ, इसलिए मेरे पास यह देखने के लिए कुछ समय था कि नाम बिल्कुल होता है, और यह करता है! जब डोमेनजॉइन होता है, तो यह पुराने नाम पर वापस आ जाता है और वह वह कम्प्यूटनाम है जो डोमेन में बनाया जाता है, न कि नया नाम।

मैंने चारों ओर खोज की है, लेकिन इस तरह की कोई समस्या नहीं मिली है, न ही इस विशेष आवश्यकता के लिए समाधान।

+0

आपने देखा है अधिकार की जरूरत है: http://stackoverflow.com/questions/6217799/rename-computer-and-join-to-domain-in-one-step-with-powershell और http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/ad11d4c0-20cd-406b-94a4-9551cdc73388/ – gordatron

+0

यह भी उपयोग में हो सकता है: http://stackoverflow.com/questions/ 4183759/कार्यक्रम tic-join-windows-machine-to-ad-domain – gordatron

+0

तो उत्तर है: डोमेन में शामिल हों, फिर पीसी का नाम बदलें। यह एक समस्या का थोड़ा सा प्रस्तुत करता है, क्योंकि गैर-व्यवस्थापक के पास एक कम्प्यूटनेम बदलने की अनुमति नहीं है, लेकिन मैं इसके आसपास काम करूंगा, मदद के लिए बहुत बहुत धन्यवाद :) –

उत्तर

2

यहाँ एसी # समारोह है कि एक डोमेन के लिए एक कार्य केंद्र मिलती है ... सोचा आप एक वैकल्पिक तरीका है कि WMI से बचा जाता है और ManagementObject बजाय का उपयोग करता है के रूप में इसका इस्तेमाल हो सकता है:

(मुझे लगता है कि क्रियान्वित साख अभी भी नाम बदलने के लिए)

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

    } 
संबंधित मुद्दे