我很乐意在Powershell中做到这一点,但这不是一个选择。我有一堆计算机,我正在寻找重命名,并试图尽可能地自动化该过程。我想要一个带有两列(oldname,newname)的csv文件设置,并能够将该信息提取到vbs脚本中以自动重命名计算机。
重命名单个计算机的代码是:
Name = "wantedcomputername"
Password = "localadminpassword"
Username = "localadminusername"
Set objWMIService = GetObject("Winmgmts:root\cimv2")
'Call always gets only one Win32_ComputerSystem object.
For Each objComputer In objWMIService.InstancesOf("Win32_ComputerSystem")
Return = objComputer.rename(Name,Password,Username)
If Return <> 0 Then
WScript.Echo "Rename failed. Error = " & Err.Number
Else
WScript.Echo "Rename succeeded." &
"Reboot for new name to have effect"
End If
Next
我也不想在更改名称后强制重启机器。重启将完成,但我不能在重命名中包含一个。我不太了解编码从csv文件中提取信息,但我很感激任何帮助或反馈。
将CSV作为数据库打开可以说是最优雅的解决方法:
filename = "C:\path\to\your.csv"
Set csv = CreateObject("Scripting.FileSystemObject").GetFile(filename)
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & csv.ParentFolder.Path & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
rs.Open "SELECT * FROM [" & csv.Name & "]", conn
Do Until rs.EOF
WScript.Echo rs.Fields("ComputerName").Value
rs.MoveNext
Loop
rs.Close
conn.Close