Using AutoIt for automatic domain addition

Source: Internet
Author: User
Tags dsquery

#Region; * * * * * * * * * * * * * * Accau3wrapper_gui #AutoIt3Wrapper_Icon =c:\users\helpdesk07\desktop\autoit script \fuwuqi.ico# Autoit3wrapper_outfile_x64= finished script \jiayuv2.exe#autoit3wrapper_res_comment= do not restart automatically modify the computer name, add domain #autoit3wrapper_res_ Legalcopyright= Sun Chao #AutoIt3Wrapper_Res_Language =2052#autoit3wrapper_res_requestedexecutionlevel=none#endregion; * <GUIConstants.au3> #include <GUIConstantsEx.au3> #include <EditConstants.au3> #include < buttonconstants.au3> #include <StaticConstants.au3> #include <windowsconstants.au3>opt (" Guicoordmode ", 1) Opt (" Trayiconhide ", 1) #CS old code Comment $gui=guicreate (" Add Domain-xx ", 300,200,300,300) $computer = Guictrlcreatelabel ("Computer name:", 20,20,50) $computer _text=guictrlcreateinput ("", 75,20,200) $user =guictrlcreatelabel (" User name: ", 20,50,50) $user _text=guictrlcreateinput (" Domain\sunchao ", 75,50,200) $pwd =guictrlcreatelabel (" Password: ", 20,80,50 ) $pwd _TEXT=GUICTRLCREATEINPUT ("Apassword", 75,80,200, "", $ES _password) $domain _name=guictrlcreatelabel ("Domain:", 20,110,50) $domain _text= Guictrlcreateinput ("www.domain.com", 75,110,200) $ou =guictrlcreatelabel ("ou:", 20,140,50) $ou _text= Guictrlcreateinput ("ou=computers,dc=domain,dc=com", 75,140,200) $ok =guictrlcreatebutton ("OK", 80,175,80) $cancel = Guictrlcreatebutton ("Cancel", 195,175,80) guisetstate (@SW_SHOW) #ce #region # # # START Koda GUI Section # # # Form=d:\ document \ au3script\ Domain V2.kxf$form2 = guicreate ("Add Domain-xx-v2.0", 494, 362, 205, 137) $computer = Guictrlcreatelabel ("Computer name", 51, 27, 48, 17  ) $computer _text = Guictrlcreateinput ("", 106, +, 193) $asuse = Guictrlcreatelabel ("Target user name", seven, four, four) $asuser _text = Guictrlcreateinput ("", 107, 193, 102) $user = Guictrlcreatelabel ("Engineer User name", "page") $user _text = Guictrlcreate Input ("", 107, 101, 193, 106) $pwd = Guictrlcreatelabel ("Engineer Password", +, 139, 136) $pwd _text = Guictrlcreateinput , 193, Bitor ($GUI _ss_default_input, $ES _password)) $domain _name = Guictrlcreatelabel ("Domain", 46, 179, (+) $domain _text = Guictrlcreateinput ("", 106, 176, 193, Bitor ($GUI _ss_default_input, $ES _password)) $ou = GU Ictrlcreatelabel ("OU", +, (+), +) $ou _text = Guictrlcreateinput ("", 106, 217, 193, Bitor ($GUI _ss_default_input,$ Es_password)) $ok = Guictrlcreatebutton ("OK", "a", "276,") $cancel = Guictrlcreatebutton ("Cancel", 211, 276, a.) guisetst Ate (@SW_SHOW) #EndRegion # # # END Koda GUI Section # # # #GUICtrlSetData ($computer _text,getoldcname ()) while 1 Local $msg = Guigetmsg () Local $result Select case $msg = $ok local $computer _r=guict Rlread ($computer _text) local $user _r=guictrlread ($user _text) Local $password _r=guictrlread ($pwd _text) local $domain _r=guictrlread ($domain _text) Local $o                         U_r=guictrlread ($ou _text) Local $asuser _r=guictrlread ($asuser _text) winsetstate ("Add Field-xx", "", @SW_HIDE) PRogresson ("Add Domain-xx", "check if necessary files exist", "" "," "", "" "," 2 ") If Not (FileExists (@SystemDir &" \dsquery.exe ") A                                nd fileexists (@SystemDir & "\netdom.exe") and FileExists (@SystemDir & "\dsquery.dll") Then                                MsgBox (4096, "hint", "Please copy Dsquery.exe, Netdom.exe, Dsquery.dll to System32 folder") Progressoff ()                        Winsetstate ("Add Domain-xx", "", @SW_SHOW) Continueloop EndIf Progressset (10, "", "Check if complete") if $computer _r= "" or $user _r= "" O                                R $password _r= "" Or $domain _r= "" Then MsgBox (4096, "Prompt", "Please fill complete!") Progressoff () winsetstate ("Add Domain-xx", "", @SW_SHOW) Continu Eloop EndIf Progressset (20, "", "Check computer name compliance rule") if not Nameisrore ($         Computer_r) Then       MsgBox (4096, "hint", "computer name does not conform to rules, can only consist of alphanumeric and hyphens, not more than 15 characters, and not all numbers!") Progressoff () winsetstate ("Add Domain-xx", "", @SW_SHOW) C Ontinueloop EndIf Progressset (30, "", "establishing connections to the domain") If No                                T Ping_domain ($domain _r) then MsgBox (4096, "Prompt", "Cannot contact domain" & $domain _r& "Please check network configuration!")                                                            Progressoff () winsetstate ("Add Domain-xx", "", @SW_SHOW) Continueloop EndIf Progressset (40,                                "", "Check if domain is joined") if not Isgord () and then MsgBox (4096, "hint", "you are already a member of the domain!")                                                            Progressoff () winsetstate ("Add Domain-xx", "", @SW_SHOW) COntinueloop EndIf Progressset (50, "", "verify that the user name and password are correct") I                                F not E_UANDP ($domain _r, $user _r, $password _r) then MsgBox (4096, "hint", "username or password wrong!")                                                            Progressoff () winsetstate ("Add Domain-xx", "", @SW_SHOW) Continueloop EndIf Progressset (60, "", "check                                Whether the computer name has been used in the domain ") if userexists ($computer _r, $domain _r, $user _r, $password _r) Then                                MsgBox (4096, "hint", "the computer name is already in use in the domain!")                                                            Progressoff () winsetstate ("Add Domain-xx", "", @SW_SHOW)                        Continueloop EndIf Progressset (70, "", "joining Domain")   If Stringupper ($computer _r) ==stringupper (Getoldcname ()) Then                             $alert =msgbox (1, "hint", "you have not modified the computer name, are you sure to join the domain with the current computer name?") If $alert >1 then Progressoff () winsetstate ("Add Domain-xx                                "," ", @SW_SHOW) Continueloop                                EndIf Else Progressset (80, "Modifying computer name")                        Rename ($computer _r) EndIf Progressset (90, "joining a domain") $result =join ($computer _r, $domain _r, $user _r, $password _r, $ou _r) If $result =1 T Henprogressset (95, "the target user is being joined to the Administrators group") If $asuser _r= "" Thenmsgbox (0, "error", "The target user name cannot be empty!")                                ") Progressoff () continueloopelsejoinadmin ($asuser _r) EndIf progressset (100," "," Finish ") Sleep (+) Progressoff ()                                Popup ("Restart", "domain successful, computer will restart after 10 seconds", Shutdown (6) Else Progressoff () winsetstate ("Add Domain-xx", "", @SW_SHOW  ) Continueloop EndIf Exit case $msg = $cancel Or $msg = $GUI _event_close exitloop endselectwendfunc ping_domain ($domain); Connect to the domain If Ping ($domain) then return True Else return False ENDIFENDFUNCFU NC getoldcname (); Gets the old computer name $obj =objcreate ("Wscript.Network") Return $obj.     Computernameendfuncfunc Isgord (); Determine if you have joined the domain $strComputer = "." $objWMIService = Objget ("winmgmts:\\" & $strComputer & "\root\cimv2") $collitems = $objWMIService. ExecQuery (" SELECT * FROM Win32_ComputerSystem ") for $colitem in $collitems Local $rule = $colitem.DomainRole If $rule =0 then Return True Else Re Turn False EndIf nextendfuncfunc userexists ($computer _name, $domainname, $username, $password), determines whether the domain has been By the same computer name Local [email protected]& "\comp.txt" If fileexists ($path) then Filedelete ($path) E NdIf runwait (@ComSpec &/C dsquery computer-name & $computer _name& "-D" & $domainname & "-u" & $u sername& "-P" & $password & > & $path, "", @SW_HIDE) If filegetsize ($path) >0 then Return T Rue Else Return False endifendfuncfunc rename ($name); Rename the computer name RegWrite ("Hklm\system\currentcontrols Et\control\computername\computername "," computername "," REG_SZ ", $name) RegWrite (" Hklm\system\currentcontrolset\ Control\computername\activecomputername "," computername "," REG_SZ ", $name) RegWrite (" HKLM\System\CurrentControlSet \services\tcpip\parameters "," NV hostname"," REG_SZ ", $name) RegWrite (" HKLM\System\CurrentControlSet\Services\Tcpip\Parameters "," hostname "," REG_SZ ", $name)          Endfunc Func Join ($computer _name, $domain, $user, $pwd, $ou); add domain Local [email protected]& "\join.txt" Local $str local $result If $ou = "then $str ="/C netdom join "& $computer _name&am                  P; "/domain:" & $domain & "/UserD:" & $user & "/PasswordD:" & $pwd & ">" & $temp Else $str = "/C netdom join" & $computer _name& "/domain:" & $domain & "/ou:" & $ou & "/UserD:" & $user & "/PasswordD:" & $pwd & ">" & $temp EndIf If MsgBox (1, "Confirm", "You confirm Join Domain" & $domain & "?")                  =1 then If fileexists ($temp) then Filedelete ($temp) EndIf Runwait (@ComSpec & $str, "", @SW_HIDE) If fileexists ($temp) then $st R=filereadline ($tempIf stringlower ($str) = = "The command completed successfully."                                  Then $result =1 Else $result =2                   MsgBox (4096, "hint", $str) EndIf EndIf Else          $result =3 EndIf Return $result endfunc Func nameisrore ($computername); Determine if the computer name conforms to the rules $result =stringregexp ($computername, ' ^[a-za-z0-9-]{1,15}$ ') $result 1=stringregexp ($computername, ' ^\d{1,15          }$ ') If $result ==1 and $result 1==0 then return True Else return False EndIf endfunc Func E_UANDP ($domain, $user, $pwd); Determine if the user name and password are correct Local [email protected]& "\net.t XT "If FileExists ($path) then Filedelete ($path) EndIf runwait (@ComSpec & '/ C net use */del/y ', "", @SW_HIDE) runwait (@ComSpec& "/c net use \ \" & $domain & "\ipc$/user:" & $user & "& $pwd &" > "& $path," ", @SW_HIDE)                  If FileExists ($path) then If filegetsize ($path) >0 then Return True Else Return False EndIf EndIf endfunc Func popup ($title , $text, $second) Local $gui =guicreate ($title, 240,60) guisetstate (@SW_SHOW) $tishi =guictrlcreatelabel ("", 10, 20,            200,15) while 1 If $second >0 then TimeSpan ($tishi, $text, $second) exitloop  EndIf Wendendfuncfunc TimeSpan ($gui, $text, $time) while $time >0 guictrlsetdata ($gui, $text & " "& $time) Sleep ($time) = $time-1 wendendfuncfunc joinadmin ($asuser) runwait (@ComSpe c& '/C net localgroup Administrators aspire\ ' & $asuser & '/add ') endfunc

Using AutoIt for automatic domain addition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.