使用autoit實現自動加域

來源:互聯網
上載者:User

標籤:

#Region ;**** 由 AccAu3Wrapper_GUI 建立指令 ****#AutoIt3Wrapper_Icon=C:\Users\helpdesk07\Desktop\autoit指令碼\fuwuqi.ico#AutoIt3Wrapper_OutFile_x64=成品指令碼\jiayuv2.exe#AutoIt3Wrapper_Res_Comment=不重起自動修改電腦名稱,加網域#AutoIt3Wrapper_Res_LegalCopyright=孫超 #AutoIt3Wrapper_Res_Language=2052#AutoIt3Wrapper_Res_requestedExecutionLevel=None#EndRegion ;**** 由 AccAu3Wrapper_GUI 建立指令 ****#Region ;**** 參數建立於 ACNWrapper_GUI ****#EndRegion ;**** 參數建立於 ACNWrapper_GUI ****#include <GUIConstants.au3>#include <GUIConstantsEx.au3>#include <EditConstants.au3>#include <ButtonConstants.au3>#include <StaticConstants.au3>#include <WindowsConstants.au3>Opt("GUICoordMode",1)Opt("TrayIconHide",1)#CS 舊代碼注釋$gui=GUICreate("加域-XX",300,200,300,300)$computer=GUICtrlCreateLabel("電腦名:",20,20,50)$computer_text=GUICtrlCreateInput("",75,20,200)$user=GUICtrlCreateLabel("使用者名稱:",20,50,50)$user_text=GUICtrlCreateInput("domain\sunchao",75,50,200)$pwd=GUICtrlCreateLabel("密碼:",20,80,50)$pwd_text=GUICtrlCreateInput("apassword",75,80,200,"",$ES_PASSWORD)$domain_name=GUICtrlCreateLabel("域:",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("確  定",80,175,80)$cancel=GUICtrlCreateButton("取  消",195,175,80)GUISetState(@SW_SHOW)#ce#Region ### START Koda GUI section ### Form=D:\文檔\au3script\加域v2.kxf$Form2 = GUICreate("加域-XX-v2.0", 494, 362, 205, 137)$computer = GUICtrlCreateLabel("電腦名", 51, 27, 48, 17)$computer_text = GUICtrlCreateInput("", 106, 25, 193, 21)$asuse = GUICtrlCreateLabel("目標使用者名稱", 27, 65, 64, 17)$asuser_text = GUICtrlCreateInput("", 107, 62, 193, 21)$user = GUICtrlCreateLabel("工程師使用者名稱", 17, 102, 76, 17)$user_text = GUICtrlCreateInput("", 107, 101, 193, 21)$pwd = GUICtrlCreateLabel("工程師密碼", 26, 139, 64, 17)$pwd_text = GUICtrlCreateInput("", 106, 136, 193, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))$domain_name = GUICtrlCreateLabel("域", 46, 179, 16, 17)$domain_text = GUICtrlCreateInput("", 106, 176, 193, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))$ou = GUICtrlCreateLabel("OU", 44, 220, 20, 17)$ou_text = GUICtrlCreateInput("", 106, 217, 193, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))$ok = GUICtrlCreateButton("確定", 45, 276, 91, 30)$cancel = GUICtrlCreateButton("取消", 211, 276, 91, 30)GUISetState(@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=GUICtrlRead($computer_text)                        Local $user_r=GUICtrlRead($user_text)                        Local $password_r=GUICtrlRead($pwd_text)                        Local $domain_r=GUICtrlRead($domain_text)                        Local $ou_r=GUICtrlRead($ou_text)Local $asuser_r=GUICtrlRead($asuser_text)                        WinSetState("加域-XX","",@SW_HIDE)                        ProgressOn("加域-XX","檢查必要檔案是否存在","","400","350",2)                        If Not(FileExists(@SystemDir&"\dsquery.exe") And FileExists(@SystemDir&"\netdom.exe") And FileExists(@SystemDir&"\dsquery.dll")) Then                                MsgBox(4096,"提示","請複製dsquery.exe、netdom.exe、dsquery.dll到system32檔案夾")                                ProgressOff()                                WinSetState("加域-XX","",@SW_SHOW)                                ContinueLoop                        EndIf                        ProgressSet(10,"","檢查是否填寫完整")                        If $computer_r="" Or $user_r="" Or $password_r="" Or $domain_r="" Then                                MsgBox(4096,"提示","請填寫完整!")                                ProgressOff()                                WinSetState("加域-XX","",@SW_SHOW)                                ContinueLoop                        EndIf                        ProgressSet(20,"","檢查電腦名稱是否符合規則")            If Not nameIsRorE($computer_r) Then                MsgBox(4096,"提示","電腦名稱不符合規則,只能由字母數字和連字號組成,不能超過15個字元,且不能全為數字!")                                ProgressOff()                                WinSetState("加域-XX","",@SW_SHOW)                                            ContinueLoop                        EndIf                        ProgressSet(30,"","建立到網域的串連")                        If Not Ping_domain($domain_r) Then                                MsgBox(4096,"提示","不能聯絡到域"&$domain_r&"請檢查網路設定!")                                ProgressOff()                                WinSetState("加域-XX","",@SW_SHOW)                                                            ContinueLoop                        EndIf                        ProgressSet(40,"","檢查是否已經加入網域")                        If Not isGorD() Then                                MsgBox(4096,"提示","你已經是域成員!")                                ProgressOff()                                WinSetState("加域-XX","",@SW_SHOW)                                                            ContinueLoop                        EndIf                        ProgressSet(50,"","驗證使用者名稱和密碼是否正確")                        If Not E_UandP($domain_r,$user_r,$password_r) Then                                MsgBox(4096,"提示","使用者名稱或密碼錯誤!")                                ProgressOff()                                WinSetState("加域-XX","",@SW_SHOW)                                                            ContinueLoop                        EndIf                        ProgressSet(60,"","檢查電腦名稱是否已經在域中使用")                        If userExists($computer_r,$domain_r,$user_r,$password_r) Then                                MsgBox(4096,"提示","該電腦名稱已經在域中使用!")                                ProgressOff()                                WinSetState("加域-XX","",@SW_SHOW)                                                            ContinueLoop                        EndIf                        ProgressSet(70,"","正在加入網域")                        If StringUpper($computer_r)==StringUpper(getOldCName()) Then                                $alert=MsgBox(1,"提示","你沒有修改電腦名稱,確認以當前電腦名稱加入網域嗎?")                                If $alert>1 Then                                    ProgressOff()                                    WinSetState("加域-XX","",@SW_SHOW)                                                                                        ContinueLoop                                EndIf                        Else                                ProgressSet(80,"正在修改電腦名稱")                                rename($computer_r)                        EndIf                        ProgressSet(90,"正在加入網域")                        $result=join($computer_r,$domain_r,$user_r,$password_r,$ou_r)                        If $result=1 ThenProgressSet(95,"正在將目標使用者加入到Administrator 群組")If $asuser_r="" ThenMsgBox(0,"錯誤","目標使用者名稱不可為空!")ProgressOff()ContinueLoopElsejoinadmin($asuser_r)EndIf                                ProgressSet(100,"","完成")                                Sleep(3000)                                ProgressOff()                                popup("重起","加域成功,電腦將在10秒後重起",10)                                Shutdown(6)                        Else                                ProgressOff()                                WinSetState("加域-XX","",@SW_SHOW)                                ContinueLoop                        EndIf                        Exit                Case $msg=$cancel Or $msg=$GUI_EVENT_CLOSE                        ExitLoop        EndSelectWEndFunc Ping_domain($domain) ;判斷能否串連到域        If Ping($domain) Then                Return True        Else                Return False        EndIfEndFuncFunc getOldCName();擷取舊的電腦名稱        $obj=ObjCreate("WScript.Network")        Return $obj.ComputerNameEndFuncFunc isGorD() ;判斷是否已經加入網域        $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                        Return False                EndIf        NextEndFuncFunc userExists($computer_name,$domainname,$username,$password) ;判斷域中是否已經存在相同的電腦名        Local [email protected]&"\comp.txt"    If FileExists($path) Then           FileDelete($path)   EndIf    RunWait(@ComSpec&" /c dsquery computer -name "&$computer_name&" -d "&$domainname&" -u "&$username&" -p "&$password&" > "&$path,"",@SW_HIDE)    If FileGetSize($path)>0 Then           Return True    Else           Return False    EndIfEndFuncFunc rename($name) ;重新命名電腦名         RegWrite("HKLM\system\currentcontrolset\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) ;加域          Local [email protected]&"\join.txt"          Local $str          Local $result          If $ou="" Then                  $str=" /c netdom join "&$computer_name&" /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,"確認","你確認加入網域 "&$domain &" ?")=1 Then                  If FileExists($temp) Then                          FileDelete($temp)                  EndIf                  RunWait(@ComSpec&$str,"",@SW_HIDE)                  If FileExists($temp) Then                          $str=FileReadLine($temp)                          If StringLower($str)=="the command completed successfully." Then                                  $result=1                          Else                                  $result=2                                  MsgBox(4096,"提示",$str)                          EndIf                  EndIf          Else                  $result=3          EndIf          Return $result  EndFunc  Func nameIsRorE($computername) ;判斷電腦名稱是否符合規則          $result=StringRegExp($computername,‘^[a-zA-Z0-9-]{1,15}$‘)          $result1=StringRegExp($computername,‘^\d{1,15}$‘)          If $result==1 And $result1==0 Then                  Return True          Else                  Return False          EndIf  EndFunc  Func E_UandP($domain,$user,$pwd)  ;判斷使用者名稱和密碼是否正確          Local [email protected]&"\net.txt"          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(1000)                $time=$time-1    WEndEndFuncFunc joinadmin($asuser)RunWait(@ComSpec&‘ /c net localgroup administrators aspire\‘&$asuser&‘ /add‘)EndFunc

 

使用autoit實現自動加域

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.