自動加域--Script
阿新 • • 發佈:2018-01-29
ade then comm ini rep col all gin 提交 自動加域--Script
說到自動加域,我們作為一個運維管理員來說,是必須要掌握了的 ,通過腳本提高管理員的工作效率,一般可以將自動加域腳本(前提是需要將腳本加密,內部有明文賬戶及密碼)通過共享的方式然後用戶手動執行,前一段時間領導也有這個需求,所以就總了一些,分享給有需要的。
一般分為兩步,首先是計算更改計算機名,其次就是加域了;最後是需要將用戶增加到管理員組中,保證用戶有足夠的權限進行操作。
首先是修改計算機名:執行會提示當前的計算機名,然後輸入新的計算機名,單擊確認即可;
說到自動加域,我們作為一個運維管理員來說,是必須要掌握了的 ,通過腳本提高管理員的工作效率,一般可以將自動加域腳本(前提是需要將腳本加密,內部有明文賬戶及密碼)通過共享的方式然後用戶手動執行,前一段時間領導也有這個需求,所以就總了一些,分享給有需要的。
一般分為兩步,首先是計算更改計算機名,其次就是加域了;最後是需要將用戶增加到管理員組中,保證用戶有足夠的權限進行操作。
首先是修改計算機名:執行會提示當前的計算機名,然後輸入新的計算機名,單擊確認即可;
Dim reval Set objnet = CreateObject ("WScript.Network") Set R = CreateObject("WScript.Shell") reval = InputBox ("Your ComputerName is:" & objnet.ComputerName,"Input Your new ComputerName") On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colComputers = objWMIService.ExecQuery _ ("Select * from Win32_ComputerSystem") For Each objComputer in colComputers errReturn = ObjComputer.Rename (reval) If reval <> "" Then return=MsgBox ("Reboot Computer?",vbokcancel+vbexclamation,"tips!") If return=vbok Then R.run("Shutdown.exe -r -t 0") End if End If Next
修改計算機名後,然後就是加域:
On Error Resume Next dim domain set domain =createobject("wscript.shell") Const JOIN_DOMAIN = 1 Const ACCT_CREATE = 2 Const ACCT_DELETE = 4 Const WIN9X_UPGRADE = 16 Const DOMAIN_JOIN_IF_JOINED = 32 Const JOIN_UNSECURE = 64 Const MACHINE_PASSWORD_PASSED = 128 Const DEFERRED_SPN_SET = 256 Const INSTALL_INVOCATION = 262144 ‘Domain Name strDomain = "ixmsoft.com" ‘administrator member password strPassword = "123" ‘administrator member strUser = "gavin" Set objNetwork =CreateObject("WScript.Network") strComputer = objNetwork.ComputerName Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\"& _ strComputer &"\root\cimv2:Win32_ComputerSystem.Name=‘" & _ strComputer & "‘") ReturnValue =objComputer.JoinDomainOrWorkGroup(strDomain, _ strPassword, strDomain & "\"& strUser, NULL, _ JOIN_DOMAIN + ACCT_CREATE) wscript.echo "Join Domain Success!"
最後就是將用戶增加到管理員組中;
dmain_user="ixmsoft\zhangsan"
Set objShell=wscript.createObject("wscript.shell")
objshell.Run "net localgroup Administrators " & domain_user & " /add",0,true
以上腳本需要執行三次,但是比較麻煩,我們最終將三個腳本的功能合成一個,
執行後,會提示修改計算機名,然後提交後,系統會重啟,重啟後,會將計算機自動加域。
On Error Resume Next Err.Clear Set oWsh = CreateObject("WScript.Shell") sRegValue = oWsh.RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion\Run\rr.vbs") If sRegValue = "" Then Dim reval_all Set objnet = CreateObject ("WScript.Network") Set R = CreateObject("WScript.Shell") reval_all = InputBox ("當前計算機名稱:" & objnet.ComputerName & chr(10)&chr(13) & "當前登錄名:" & objnet.username & chr(10)&chr(13) &"請輸入新的計算機名和登錄賬戶,用”,“分開.(例子:zhangsan-pc,zhangsan)" & chr(10)&chr(13) & "註:登陸賬戶為登陸此計算機的域賬戶。" ,"請輸入計算名和登錄名") arr1 = split(reval_all,",") reval = arr1(0) On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colComputers = objWMIService.ExecQuery _ ("Select * from Win32_ComputerSystem") For Each objComputer in colComputers errReturn = ObjComputer.Rename (reval) If reval <> "" Then Dim AutoRunProgram Set AutoRunProgram = WScript.CreateObject("WScript.shell") RegPath ="HKLM\Software\Microsoft\Windows\CurrentVersion\Run\" Type_Name = "REG_SZ" Key_Name = "rr.vbs" strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * From Win32_Process Where Name = ‘wscript.exe‘ OR Name = ‘cscript.exe‘") For Each objItem in colItems lmq= objItem.CommandLine lmq= replace(lmq,Chr(34),",") arr = split(lmq,",") Next Key_data = arr(3) AutorunProgram.RegWrite regpath&Key_Name,Key_data,Type_Name AutorunProgram.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\loginName",arr1(1),Type_Name return=MsgBox ("Please Reboot Computer?",vbokcancel+vbexclamation,"tips!") If return=vbok Then R.run("Shutdown.exe -r -t 0") End if End If Next End If if sRegValue <> "" then dim domain set domain =createobject("wscript.shell") Const JOIN_DOMAIN = 1 Const ACCT_CREATE = 2 Const ACCT_DELETE = 4 Const WIN9X_UPGRADE = 16 Const DOMAIN_JOIN_IF_JOINED = 32 Const JOIN_UNSECURE = 64 Const MACHINE_PASSWORD_PASSED = 128 Const DEFERRED_SPN_SET = 256 Const INSTALL_INVOCATION = 262144 ‘Domain Name strDomain = "ixmsoft.com" ‘administrator member password strPassword = "Bey0d101" ‘administrator member strUser = "addtodomain" Set objNetwork =CreateObject("WScript.Network") strComputer = objNetwork.ComputerName Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\"& _ strComputer &"\root\cimv2:Win32_ComputerSystem.Name=‘" & _ strComputer & "‘") ReturnValue =objComputer.JoinDomainOrWorkGroup(strDomain, _ strPassword, strDomain & "\"& strUser, NULL, _ JOIN_DOMAIN + ACCT_CREATE) dim wsh set wsh=wscript.createobject("wscript.shell") wsh.regdelete "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\rr.vbs" wscript.echo "Join Domain Success!" Dim oWshNetwork Set oWshNetwork = WScript.CreateObject("WScript.Network") strComputer = oWshNetwork.ComputerName sFlag=0 On Error Resume Next ‘Set colGroups = GetObject("WinNT://" & strComputer & "/Administrators") ‘For Each objUser In colGroups.Members ‘Next strTestString = "/" & strComputer & "/" Set colGroups = GetObject("WinNT://" & strComputer & "/Administrators") For Each objUser In colGroups.Members If InStr(objUser.AdsPath, strTestString) Then ‘Wscript.Echo "Local user: " & objUser.Name ‘msgbox(objUser.ADsPath) Else IF objuser.Name = "DomainUser" Then sFlag=1 end if End If Next if sFlag=0 then ‘Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators") ‘ Set oWsh = CreateObject("WScript.Shell") reval = oWsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\loginName") wsh.regdelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\loginName" adspath = "WinNT://ixmsoft/" & reval ‘msgbox(adspath) Set objUser = GetObject(adspath) colGroups.Add(objUser.ADsPath) end if wscript.echo "Added Finish" end if
自動加域--Script