Good morning,
I have the following script (feel free to improve/comments so I can learn).
My problem is that it uses a text file to give it a series of computername to
run the script against. The issue is that when a computer is turned off, the
script returns an error. I would like to trap that error and then move on to
the next computer in the text file. Could someone guide me as to why my
error handler does not work.
'*********************VBS Code starts here*******************
On Error GoTo ErrHandler
ForReading = 1
strNewFile = "PCListing01.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strNewFile, ForReading)
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
'Set objOption = Document.createElement("OPTION")
'objOption.Text = strLine
'objOption.value = strLine
'CmptName.add(objOption)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from
Win32_OperatingSystem")
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now())
strHTML = strHTML & strComputer & " has been up for " &
dtmSystemUptime & " hrs" & vbcrlf
'Wscript.Echo strComputer
'Wscript.Echo dtmBootup
'Wscript.Echo dtmLastBootupTime
'Wscript.Echo dtmSystemUptime
Next
Loop
objFile.Close
Wscript.Echo strHTML
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & Mid(dtmBootup,
7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & Mid(dtmBootup, 11, 2) & ":" &
Mid(dtmBootup, 13, 2))
End Function
ErrHandler: ' this is a named range called ErrHandler
' If there was an error raised by the function we would wind up in here 'so
display a message to the user to 'show what went wrong
if err.number=0x80041003 then
strHTML = strHTML & "Unable to reach " & strComputer
Next ObjFile
Resume Next
else
'MsgBox "An error happened here!" & vbCrLf & _
'"Error number: " & Err.Number & vbCrLf & _
'"Error Description" & Err.Description & vbCrLf & _
'"Error Help Context ID" & Err.HelpContext & vbCrLf & _
'"Error Help File" & Err.HelpFile, vbCritical, "ERROR MESSAGE"
'Err.Clear ' destroy the error andâ?¦
' â?¦ this resumes in the last known position.
' in this case, since its not more than a file opening occurring,
' it will simply exit the function for you.
Resume Next
end if
'*********************VBS Code ends here*******************
Thanks,
Daniel