AlainN Posted April 14, 2008 Posted April 14, 2008 Hello,I use WMI to retrieve OS version from remote computers . When one remote Pc is unreachable (off line, firewall, RPC service, ...) I get an error :Variable must be of type : "Object"and the script fails.I would like to know how to handle this error case and continue the script like VBS "on error resume next" or "on error goto".Here his a script sample : $wbemFlagReturnImmediately = 0x10$wbemFlagForwardOnly = 0x20$colItems = ""$strComputer = InputBox("Computer", "Name : ", "")$Output=""$Output = $Output & "Computer: " & $strComputer & @CRLF$Output = $Output & "==========================================" & @CRLF$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly)If IsObj($colItems) then For $objItem In $colItems $Output = $Output & "Version: " & $objItem.Version & @CRLF if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop $Output="" NextElse Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_OperatingSystem" )EndifThank's for your help.<AN>
rover Posted April 15, 2008 Posted April 15, 2008 Welcome to the forums use the AutoIt COM error handler look up 'COM Error Handling' in the help file in 'Function Reference' section, subsection 'Obj/COM Reference' here's an example with a COM error handler and use of 'IsObj()' to verify objects. you may want to loop through machine names read from an ini or text file and log the results to a file. expandcollapse popupGlobal $oMyError = ObjEvent("AutoIt.Error", "ComErrorHandler") If Not IsObj($oMyError) Then MsgBox(0, "AutoIt.Error", "Problem with COM error handler") Exit EndIf Global $wbemFlagReturnImmediately = 0x10 Global $wbemFlagForwardOnly = 0x20 Global $wbemFlag = BitOR($wbemFlagForwardOnly, $wbemFlagReturnImmediately) Global $colItems = "" Global $Output = "" Global $strComputer, $ErrMsg Do $strComputer = InputBox("Computer", "Name : " & $ErrMsg, "") ; cancel exits If @error Then Exit $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") ; check @error returned by COM error handler if machine is unreachable or does not exist If Not @error And IsObj($objWMIService) Then ; can also check if variable is object Beep(1000, 5) ExitLoop Else $ErrMsg = @TAB & @TAB & $strComputer & @CRLF & "COM Error: " & @TAB & $oMyError.windescription EndIf Until Not @error $Output &= "Computer: " & $strComputer & @CRLF $Output &= "==========================================" & @CRLF ; next line limits objects in collection to just 'Version'. can add additional objects separated by commas ;e.g. "SELECT Caption,Version FROM Win32_OperatingSystem" $colItems = $objWMIService.ExecQuery("SELECT Version FROM Win32_OperatingSystem", "WQL", $wbemFlag) If IsObj($colItems) Then For $objItem In $colItems ;$Output &= "Caption: " & $objItem.Caption & @CRLF $Output &= "Version: " & $objItem.Version & @CRLF If MsgBox(1, "WMI Output", $Output) = 2 Then ExitLoop $Output = "" Next Else MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_OperatingSystem") EndIf Func ComErrorHandler() $HexNumber = Hex($oMyError.number, 8) ConsoleWrite("-> Debug: Intercepted COM Error: Number = " & $HexNumber & _ " Windescription = " & $oMyError.windescription & @LF) ;MsgBox(0, "COM Error", "We intercepted a COM Error !" & @CRLF & @CRLF & _ ;"err.description is: " & @TAB & $oMyError.description & @CRLF & _ ;"err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _ ;"err.number is: " & @TAB & $HexNumber & @CRLF & _ ;"err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _ ;"err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _ ;"err.source is: " & @TAB & $oMyError.source & @CRLF & _ ;"err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _ ;"err.helpcontext is: " & @TAB & $oMyError.helpcontext _ ;, 5) ; timeout after 5 seconds SetError(1) ; to check for after this function returns EndFunc ;==>COMErrFunc I see fascists...
AlainN Posted April 15, 2008 Author Posted April 15, 2008 Welcome to the forums use the AutoIt COM error handler look up 'COM Error Handling' in the help file in 'Function Reference' section, subsection 'Obj/COM Reference' here's an example with a COM error handler and use of 'IsObj()' to verify objects. you may want to loop through machine names read from an ini or text file and log the results to a file. expandcollapse popupGlobal $oMyError = ObjEvent("AutoIt.Error", "ComErrorHandler") If Not IsObj($oMyError) Then MsgBox(0, "AutoIt.Error", "Problem with COM error handler") Exit EndIf Global $wbemFlagReturnImmediately = 0x10 Global $wbemFlagForwardOnly = 0x20 Global $wbemFlag = BitOR($wbemFlagForwardOnly, $wbemFlagReturnImmediately) Global $colItems = "" Global $Output = "" Global $strComputer, $ErrMsg Do $strComputer = InputBox("Computer", "Name : " & $ErrMsg, "") ; cancel exits If @error Then Exit $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") ; check @error returned by COM error handler if machine is unreachable or does not exist If Not @error And IsObj($objWMIService) Then ; can also check if variable is object Beep(1000, 5) ExitLoop Else $ErrMsg = @TAB & @TAB & $strComputer & @CRLF & "COM Error: " & @TAB & $oMyError.windescription EndIf Until Not @error $Output &= "Computer: " & $strComputer & @CRLF $Output &= "==========================================" & @CRLF ; next line limits objects in collection to just 'Version'. can add additional objects separated by commas ;e.g. "SELECT Caption,Version FROM Win32_OperatingSystem" $colItems = $objWMIService.ExecQuery("SELECT Version FROM Win32_OperatingSystem", "WQL", $wbemFlag) If IsObj($colItems) Then For $objItem In $colItems ;$Output &= "Caption: " & $objItem.Caption & @CRLF $Output &= "Version: " & $objItem.Version & @CRLF If MsgBox(1, "WMI Output", $Output) = 2 Then ExitLoop $Output = "" Next Else MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_OperatingSystem") EndIf Func ComErrorHandler() $HexNumber = Hex($oMyError.number, 8) ConsoleWrite("-> Debug: Intercepted COM Error: Number = " & $HexNumber & _ " Windescription = " & $oMyError.windescription & @LF) ;MsgBox(0, "COM Error", "We intercepted a COM Error !" & @CRLF & @CRLF & _ ;"err.description is: " & @TAB & $oMyError.description & @CRLF & _ ;"err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _ ;"err.number is: " & @TAB & $HexNumber & @CRLF & _ ;"err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _ ;"err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _ ;"err.source is: " & @TAB & $oMyError.source & @CRLF & _ ;"err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _ ;"err.helpcontext is: " & @TAB & $oMyError.helpcontext _ ;, 5) ; timeout after 5 seconds SetError(1) ; to check for after this function returns EndFunc ;==>COMErrFunc Wonderfull ! Many thanks ! It works fine. I tried the AutoIt COM error handler but without succes because I placed the check in a wrong place (after the query, too late... ). Your example is very complete and usefull. In fact, I get my computers list from Active Directory and I need to know the OS version to select the right way to continue with each of them. Thanks for the councils (Select limit). <AN>
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now