Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with error in loop

Status
Not open for further replies.

ttstech

Programmer
May 13, 2015
1
US
Hello,

I am fairly new to programming in VbScript and using WMI. 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.

Please take a look at this code and tell me what maybe causing the problem. If there is a better way to write the code…I would appreciate any help you can give.

BH
_________
On Error Resume Next
const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile= objFSO.OpenTextFile _
("C:\users\bhowerton\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
 
On Error Resume Next suppresses runtime errors (rather than dealing with them properly), so don't use it as the first line of your script or you will get unexpected results. Proper error trapping in vbscript means surrounding lines that can give a runtime error with something like this:

Code:
[COLOR=blue]On Error Resume Next[/color] [COLOR=#4E9A06]'prevent runtime errors from stopping the script[/color]
[i]line of code that can give a runtime error[/i]
[COLOR=blue]If Err.Number <> 0 Then[/color]
   [i]handle the error[/i]
[COLOR=blue]End If
On Error Goto 0[/color] [COLOR=#4E9A06]'allow runtime errors to stop the script[/color]

It's probably the GetObject that is giving the error, so look for an error after that line and act accordingly. Something like this, not tested:

Code:
[COLOR=blue]On Error Resume Next[/color]
Set colSettings = objWMIService.ExecQuery _
   ("Select * from Win32_OperatingSystem")
[COLOR=blue]If Err.Number <> 0 [COLOR=#4E9A06]'Errors found[/color]
   wscript.echo strComputer & " could not be found."
   
Else [COLOR=#4E9A06]'No errors found[/color]
   On Error Goto 0[/color]
   For Each objOperatingSystem in colSettings
      Wscript.Echo strComputer & ": " & objOperatingSystem.Caption
   Next
[COLOR=blue]End If
On Error Goto 0[/color]
 
The first thing I would do is check if the machine is reachable. If a system is offline your code will fail. Here is a simple example of how to check if online. You could move your code to inside the If statement.

Code:
Function PingStatus(strComputer)

    On Error Resume Next
    
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
    For Each objPing in colPings
        Select Case objPing.StatusCode
            Case 0 PingStatus = "Success"
            Case Else PingStatus = "Failure"
        End Select
    Next

End Function 

If PingStatus("localhost") = "Success" Then
	WScript.Echo "Online"
Else
	WScript.Echo "Offline"
End If

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top