我想评估连接到我的域的计算机的内存和其他详细信息。我正在做的是在一个文本文件中写入计算机名称,每行一个。脚本将逐个读取文件(主机名),收集信息并将其写入文件。这很好。
问题是,如果一台计算机不可用,那么它就会出现问题。例如,如果第一个主机名可用且第二个主机名不可用,则它会不断重复显示相同的信息。
INPUT_FILE_NAME = "D:\tmp\Computer.txt"
Const FOR_READING = 1
Const HKEY_LOCAL_MACHINE = &H80000002
strRegKey = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
For Each strComputer In arrComputers
On Error Resume Next
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!" & _
"//./root/default:StdRegProv")
objReg.GetStringValue HKEY_LOCAL_MACHINE, strRegKey, "Hostname", strHostname
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPageFiles = objWMIService.ExecQuery _
("Select * from Win32_PageFileUsage")
For each objPageFile in colPageFiles
Wscript.Echo "Host Name: " & strHostName, _
"AllocatedBaseSize: "& vbTab & objPageFile.AllocatedBaseSize, _
"CurrentUsage: "& vbTab & objPageFile.CurrentUsage, _
"Description: "& vbTab & objPageFile.Description, _
"InstallDate: "& vbTab & objPageFile.InstallDate, _
"Name: " & vbTab & objPageFile.Name, _
"PeakUsage: " & vbTab & objPageFile.PeakUsage
Next
Next
答案 0 :(得分:2)
当GetObject()
失败时,变量objWMIService
会保留其先前的值,因此您会一遍又一遍地报告同一个主机,直到GetObject()
可以连接到主机或循环终止。改变这个:
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPageFiles = objWMIService.ExecQuery _
("Select * from Win32_PageFileUsage")
For each objPageFile in colPageFiles
Wscript.Echo "Host Name: " & strHostName, _
"AllocatedBaseSize: "& vbTab & objPageFile.AllocatedBaseSize, _
"CurrentUsage: "& vbTab & objPageFile.CurrentUsage, _
"Description: "& vbTab & objPageFile.Description, _
"InstallDate: "& vbTab & objPageFile.InstallDate, _
"Name: " & vbTab & objPageFile.Name, _
"PeakUsage: " & vbTab & objPageFile.PeakUsage
Next
进入这个:
Set objWMIService = Nothing
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Not objWMIService Is Nothing Then
Set colPageFiles = objWMIService.ExecQuery _
("Select * from Win32_PageFileUsage")
For each objPageFile in colPageFiles
Wscript.Echo "Host Name: " & strHostName, _
"AllocatedBaseSize: "& vbTab & objPageFile.AllocatedBaseSize, _
"CurrentUsage: "& vbTab & objPageFile.CurrentUsage, _
"Description: "& vbTab & objPageFile.Description, _
"InstallDate: "& vbTab & objPageFile.InstallDate, _
"Name: " & vbTab & objPageFile.Name, _
"PeakUsage: " & vbTab & objPageFile.PeakUsage
Next
Else
WScript.Echo strComputer & " unavailable."
End If
问题就会消失。
另一方面,外部循环中的前两行将始终检索本地计算机的主机名:
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!" & _
"//./root/default:StdRegProv")
objReg.GetStringValue HKEY_LOCAL_MACHINE, strRegKey, "Hostname", strHostname
如果那是你真正想要的,你应该把代码移到循环之外,因为strHostname的值不会改变:
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!" & _
"//./root/default:StdRegProv")
objReg.GetStringValue HKEY_LOCAL_MACHINE, strRegKey, "Hostname", strHostname
For Each strComputer In arrComputers
'...
Next
如果您确实需要远程计算机的名称(当其余信息来自远程计算机时会更有意义),您只需使用strComputer
并删除注册表查询完全。