自动加域--Script
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动加域--Script相关的知识,希望对你有一定的参考价值。
自动加域--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的主要内容,如果未能解决你的问题,请参考以下文章
PowerShell小技巧:通过Powershell定期自动将加域的客户端从 Computers 按规则移动至相应 OU并发送通知至机器人
公司加域的电脑,今天登录时候cisco anyconnect 出现"vpn service not available",求如何解决