Reputation: 31
I have a program that reads hostnames from a txt file, it scans the network for the hostname and then it displays the hostname and its respective Windows Operating system (CAPTION).
I am trying to get all Windows XP machines upgraded to Windows 7. I am trying to run this list to give me an idea of how many machines that I have upgraded and an idea of how many I still have to upgrade, etc.
The problem is that when I use the statement On Error Resume Next
if the script tries to contact a hostname which is a BAD HOST or if the hostname is DOWN it displays the operating system from the last hostname. Then each and every name that is scans moving forward all shows that same operating system.
What might be causing this error?
On Error Resume Next
const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile= objFSO.OpenTextFile _
("C:\users\bh\desktop\hostnames.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.close
arrComputers = Split(strText, vbCrlf)
for Each strComputer in arrComputers
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colSettings
Wscript.Echo strComputer & ": " & objOperatingSystem.Caption
Next
Next
Upvotes: 3
Views: 148
Reputation: 38745
Using a Global On Error Resume Next
is just asking for desaster - all errors will be ignored, assignment won't be done as you expect and stale data will be used.
This:
Dim aErr
arrComputers = Split(". winxpsp3 nix")
for Each strComputer in arrComputers
On Error Resume Next
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
aErr = Array(Err.Number, Err.Description)
On Error Goto 0
If 0 = aErr(0) Then
Set colSettings = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colSettings
Wscript.Echo strComputer & ": " & objOperatingSystem.Caption
Next
Else
WScript.Echo "can't reach", strComputer, "error:", Join(aErr)
End If
Next
output:
cscript 30223065.vbs
.: Microsoft Windows XP Professional
winxpsp3: Microsoft Windows XP Professional
can't reach nix error: 462 The remote server machine does not exist or is unavailable
demonstrates a strictly local error handling (max one risky operation between OERN and OEG0) for your first risky task. You'll have to guard/wrap the others accordingly or check return values.
(see this for a strategy for global error handling)
Upvotes: 1