出现Provider Can't be found
错误。此脚本用于重命名域中的工作站,同时将旧名称和新名称记录在 csv 文件中。
更新:脚本似乎已完成,但未向 .csv 写入任何内容。它也没有重命名计算机。
' RenamePC.vbs - Windows Logon Script.
' VBScript - Rename Workstation and Log Change
' Author Chris Collins
' Version 1 - July 2018
' ----------------------------------------------------------'
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Dim sCmpName,sUserType
Dim sUser,sPassword,sDomain,sOU
Dim strComputer,strSN,objWMIService,objBIOS,colSMBIOS,objShell
sUser = "administrator"
sPassword = "Password123"
sDomain = "Domain"
sOU = "OU=New Computers,DC=Domain,DC=k12,DC=az,DC=us"
' ************** Setup **************
' Folder where CSV File is located
' CSV file should have 1st field = oldname, 2nd field = newname with no header row
strCSVFolder = "E:\VBScripts\TestFolder\"
' CSV filename
strCSVFile = "computerrename.csv"
' Additional parameters to pass to NetDom command
strNetDomParams = " /userd:"+sUser+" /passwordd:"+sPassword+" /usero:"+sUser+" /passwordo:"+sPassword+" /force "
'************************************
'Part 1 - Acquire serial/service tag number
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' move the objShell creation to here to ensure it get created
Set objShell = CreateObject("Shell.Application")
Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
strSN = objSMBIOS.SerialNumber
If strSN <> "" Then exit For
Next
'Part 2 - Ask for Campus
sCmpName = InputBox("Enter the Campus the computer is on (BB, DO, HCS, THS, WJM):", "Campus Name")
If sCmpName = "" Then
Wscript.Echo "Exiting script."
Wscript.Quit
End If
'Part 3 - Ask for Type
sUserType = InputBox("Enter the User Type (CR, LAB, LIB, LP, NB, STAFF, TH). Insert other tags as well (ie -RF):", "User Type")
If sUserType = "" Then
Wscript.Echo "Exiting script."
Wscript.Quit
End If
'Part 4 - Assign computer name to serial/service tag number
Dim oWMI, oCmp, oOS, sReturn
Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
For Each oCmp in oWMI.InstancesOf("Win32_ComputerSystem")
strNewName = oCmp.Rename (+sCmpName+"-"+sUserType+"-"+strSN)
If sReturn <> 0 Then
Wscript.Echo "Rename failed. Error = " & Err.Number & _
vbcrlf & "Exiting script."
Else
Wscript.Echo "Rename successful."
strNewName = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, _
sDomain & "\" & sUser, sOU, JOIN_DOMAIN+ACCT_CREATE)
If strNewName <> 0 Then
Wscript.Echo "Join domain failed. Error = " & Err.Number & _
vbcrlf & "Exiting script."
Else
Wscript.Echo "Join domain successful."
'Part 4 - Setup ADO Connection to CSV file
SET objShell = wscript.CREATEOBJECT("wscript.shell")
SET cn = CREATEOBJECT("ADODB.Connection")
SET rs = CREATEOBJECT("ADODB.Recordset")
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strCSVFolder & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited"""
rs.Open "SELECT * FROM [" & strCSVFile & "]", _
cn, adOpenStatic, adLockOptimistic, adCmdText
DO until rs.eof
strOldName = rs(0)
strNewName = rs(1)
strCmd = "cmd.exe /C netdom renamecomputer " & strOldName & " /newname:" & strNewName & strNetDomParams
intReturn = objShell.Run(strCmd,intWindowStyle,blnWait)
IF intReturn = 0 THEN
wscript.echo "Renamed '" & strOldName & "' to '" & strNewName & "'"
ELSE
wscript.echo "Error renaming '" & strOldName & "' to '" & strNewName & "'"
END IF
rs.movenext
Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
err = objComputer.Rename(strNewName)
Next
For Each oOS in oWMI.InstancesOf("Win32_OperatingSystem")
sReturn = oOS.Reboot
Next
Loop
End If
'Step 5 - Reboot Computer
Wscript.Echo "Rebooting computer..."
Dim oShell
Set oShell = CreateObject("WScript.Shell")
'restart, wait 0 seconds, force running apps to close
oShell.Run "%comspec% /c shutdown /r /t 0 /f", , TRUE
End If
Next