×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

help with error in loop

help with error in loop

help with error in loop

(OP)
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

RE: help with error in loop

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

On Error Resume Next 'prevent runtime errors from stopping the script
line of code that can give a runtime error
If Err.Number <> 0 Then
   handle the error
End If
On Error Goto 0 'allow runtime errors to stop the script 

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

On Error Resume Next
Set colSettings = objWMIService.ExecQuery _
   ("Select * from Win32_OperatingSystem")
If Err.Number <> 0 'Errors found
   wscript.echo strComputer & " could not be found."
   
Else 'No errors found
   On Error Goto 0
   For Each objOperatingSystem in colSettings
      Wscript.Echo strComputer & ": " & objOperatingSystem.Caption
   Next
End If
On Error Goto 0 

RE: help with error in loop

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 --> VBScript

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 http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close