Sign in to follow this  
Followers 0
iviikel

WMI Error Handling

2 posts in this topic

#1 ·  Posted (edited)

So I put together this script from others (thank you) that is recording the monitors connected to our domain computers for occasional inventory purposes. The script is working exactly how I intended it to except for one aspect. If the script attempts the WMI query on a computer that WMI is broken on, the script completely bombs. What I would like to do is if such error occurs that the script just moves to the next entry in the array. I had tried to create similar error handling based off other posts but nothing seems to be working.

#Include <_SQL.au3>
#include <EditConstants.au3>
#Include <AD.au3>
#include <File.au3>
#include <Array.au3>
Dim $arrCU[5], $compName,$edidarray, $oErrors
Dim $info[5],$ser[10],$Type[10], $count
Global $debug = 0
$update = ""
Func _exit()
    Exit
EndFunc
HotKeySet("!q","_exit")
_main()
Func _Main()
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory Example Skript", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)

Global $iReply = MsgBox(308, "Monitor Inventory", "This script inventories monitors to a database, Log can be found at C:\Temp\ADLOG\log.log." & @CRLF & @CRLF & _
        "")
If $iReply <> 6 Then Exit

$aOdject = _AD_GetObjectsInOU("OU=Computers_**********,OU=Computers_*********,DC=******,DC=******")
_AD_Close()
_ArrayDisplay($aOdject)
;~ For $i = 1 to UBound($aOdject)
For $i = 796 to 1047
    $count = $i
    $test = ""
$objErr = ObjEvent("AutoIt.Error","MyErrFunc")
;~ For $i =  to 270
    If $aOdject[$i] = "" Then
        MsgBox(1,"Complete","Finished")
        Exit
    Else
    $compName = StringReplace($aOdject[$i],"$","")
    SplashTextOn("Processing", "Updating: " & $compName & "     " & $i,250, 50, -1, -1, 18)
        $PingResult = Ping($compName,4000)
    If $PingResult Then
        monitorInfo()
    Else
        EndIf
    EndIf
Next
$file = FileOpen("C:\Temp\ADLOG\Log.log",1)
FileWrite($file,"TOTAL UPDATED = " & $update)
FileWrite($file, @CRLF)
FileClose($file)
MsgBox(1,"","Finished")
EndFunc
Func monitorInfo()
$file = FileOpen("\\hodata\eut\test_installs\MonitorInfo\Log.log",1)
            FileWrite($file,$count & "     " & $compName & "     " &  @MON & "/" & @WDAY & "/" & @YEAR & "     " & @HOUR & ":" & @MIN & ":" & @SEC)
            FileWrite($file, @CRLF)
            FileClose($file)
$objWMIService = ObjGet("winmgmts:\\" & $compName & "\root\cimv2")
$colBios = $objWMIService.execquery("Select * From Win32_BIOS")
For $objBios In $colBios
        $Serial = $objBios.SerialNumber
Next
$GUIProcess = GUICtrlCreateLabel("Processing", 600, 25, 100, 25)
$iCounterEDID = 0
Dim $asEDID[1]

$iCounterMonitorName = 1
Do
    $sMonitorName = RegEnumKey("\\" & $compName & "\HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY", $iCounterMonitorName)
    If $sMonitorName <> "" Then
        $iCounterMonitorCode = 1
        Do
           ; Search 'monitor code' - 5&3aba5caf&0&10000080&01&00
            $sMonitorCode = RegEnumKey("\\" & $compName & "\HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY\" & $sMonitorName, $iCounterMonitorCode)
           ; Search Control folder - When available, the active monitor is found
            $iCounterMonitorControlFolder = 1
            Do
                $sMonitorControlFolder = RegEnumKey("\\" & $compName & "\HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY\" & $sMonitorName & "\" & $sMonitorCode, $iCounterMonitorControlFolder)
                If $sMonitorControlFolder == "Control" Then; Active monitor found!
                    $sMonitorEDIDRead = RegRead("\\" & $compName & "\HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY\" & $sMonitorName & "\" & $sMonitorCode & "\Device Parameters", "EDID")
                    If $sMonitorEDIDRead <> "" Then
                        $sMonitorEDID = $sMonitorEDIDRead
                        $iCounterEDID = $iCounterEDID + 1
                        $asEDID[0] = $iCounterEDID
                        ReDim $asEDID[UBound($asEDID) + 1]
                        $asEDID[UBound($asEDID) - 1] = $sMonitorEDID; Add found EDID string to Array
                    EndIf
                EndIf
                $iCounterMonitorControlFolder = $iCounterMonitorControlFolder + 1; Increase counter to search for next folder
            Until $sMonitorControlFolder == ""
            $iCounterMonitorCode = $iCounterMonitorCode + 1; Increase counter to search for next 'monitor code' folder
        Until $sMonitorCode == ""
    EndIf
    $iCounterMonitorName = $iCounterMonitorName + 1; Increase counter to search for next monitor
Until $sMonitorName == ""


; Decrypt collected EDID strings - Thanks archrival
$z = 0
For $k = 1 To $asEDID[0]
    $sMonEDID = $asEDID[$k]
    If $sMonEDID <> "" Then
        $j = 0
        Dim $edidarray[StringLen($sMonEDID) ]
        $edidarray[0]= (StringLen($sMonEDID) / 2) + 1
        For $i = 1 To StringLen($sMonEDID) Step 2
            $j = $j + 1
            $edidarray[$j] = Dec(StringMid($sMonEDID, $i, 2))
        Next
    $ser[$z] = StringStripWS(_FindMonitorSerial(), 1 + 2)
    $Type[$z] = StringStripWS(_FindMonitorName(), 1 + 2)
    Else
    $ser[$z] = ""
    $Type[$z] = ""
EndIf
$z=$z+1
Next
_SQL_Open_EUT()
    IF _SQL_Execute(-1, "INSERT INTO dbo.MonitorInfo (ComputerName, ComputerSerial, Monitor1, Desc1, Monitor2, Desc2, Monitor3, Desc3, Monitor4, Desc4, Monitor5, Desc5) VALUES ('" & $compName & "', '" & $serial & "', '" & $ser[0] & "', '" & $Type[0] & "', '" & $Ser[1] & "', '" & $Type[1] & "', '" & $ser[2] & "', '" & $Type[2] & "', '" & $ser[3] & "', '" & $Type[3] & "', '" & $ser[4] & "', '" & $Type[4] & "');") = $SQL_ERROR Then MsgBox(0 + 16 + 262144, "Error - line " & @ScriptLineNumber, _SQL_GetErrMsg())
_SQL_Close()
For $n = 0  to UBound($ser) - 1
    $ser[$n]=""
    $type[$n]=""
Next
    GUIctrlDelete($GUIProcess)
;~  Next
EndFunc
Func _FindMonitorSerial(); Thanks archrival
    $sernumstr = ""
    $sernum = 0
    For $i = 1 To (UBound($edidarray) / 2) - 4
        If $edidarray[$i] = "0" And $edidarray[$i + 1] = "0" And $edidarray[$i + 2] = "0" And $edidarray[$i + 3] = "255" And $edidarray[$i + 4] = "0" Then
            $sernum = $i + 4
        EndIf
    Next
    If $sernum <> 0 Then
        $endstr = 0
        $sernumstr = ""
        For $i = 1 To 13
            If $edidarray[$sernum + $i] = "10" Then
                $endstr = 1
            Else
                If $endstr = 0 Then
                    $sernumstr = $sernumstr & Chr($edidarray[$sernum + $i])
                EndIf
            EndIf
        Next
    Else
        $sernumstr = ""
    EndIf
    Return $sernumstr
EndFunc  ;==>_FindMonitorSerial

Func _FindMonitorName(); Thanks archrival
    $name = 0
    For $i = 1 To (UBound($edidarray) / 2) - 4
        If $edidarray[$i] = "0" And $edidarray[$i + 1] = "0" And $edidarray[$i + 2] = "252" And $edidarray[$i + 3] = "0" Then
            $name = $i + 3
        EndIf
    Next
    If $name <> 0 Then
        $endstr = 0
        $namestr = ""
        For $i = 1 To 13
            If $edidarray[$name + $i] = "10" Then
                $endstr = 1
            Else
                If $endstr = 0 Then
                    $namestr = $namestr & Chr($edidarray[$name + $i])
                EndIf
            EndIf
        Next
    Else
        $namestr = ""
    EndIf
    Return $namestr
EndFunc
Func _SQL_Open_EUT()
    $oADODB = _SQL_Startup()
    If $oADODB = $SQL_ERROR Then MsgBox(0 + 16 + 262144, @ScriptLineNumber, _SQL_GetErrMsg())
            If _SQL_Connect(-1, "HOPC2800\EUT", "EUT", "**", "*********") = $SQL_ERROR Then
                MsgBox(0 + 16 + 262144, "Error - Line " & @ScriptLineNumber, _SQL_GetErrMsg())
                _SQL_Close()
            EndIf
EndFunc
Edited by iviikel

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

iviikel,

Take out this statement

$objErr = ObjEvent("AutoIt.Error", "MyErrFunc")

and after this statement

$objWMIService = ObjGet("winmgmts:\\" & $compName & "\root\cimv2")

add some error checking (either @error or IsOBJ).  If there is an error just return.

After every call that creates an object you should be checking that the object exists.

kylomas

edit: additional info

If you want to use the AutoIt error object then you need to add a routine (MyErrFunc) to handle results.  You can then return a value to decide what to do.  There is an example in the Help file and many examples on the forum. 

Finally,

This

 the script completely bombs

is hardly decriptive enough to give any real help.  Please be more specific in the future.

edit2:

Here is a small example of using some error handling...

#include <array.au3>

local $aErrRet[3]
Local $oErrObj = ObjEvent("AutoIt.Error", "_ErrFunc")

;local $compname = 'ComputerThatDoesNotExist' ; <--- use this to generate error
local $compname = '.'   ; <--- use this to prove it works

local $oWMI = objget("winmgmts:\\" & $compname & "\root\cimv2")
if @error  then
    _arraydisplay($aErrRet, 'ObjGet ERROR for ' & $compname)
endif

local $cQuery = $oWMI.ExecQuery("SELECT * FROM Win32_Biosr") ; <---- use this to generate 0 count for query
;local $cQuery = $oWMI.ExecQuery("SELECT * FROM Win32_Bios") ; <---- use this to prove it works
;local $cQuery = $oWMI.ExeQuery("SELECT * FROM Win32_Bios") ; <---- use this to generate a COM error
if @error then _arraydisplay($aErrRet)
if $cQuery.count = 0 then ConsoleWrite('Nothing found for query' & @CRLF)

For $oItem In $cQuery
    For $oProp In $oItem.Properties_
        consolewrite($oProp.Name & ' = ' & $oProp.value & @LF)
    Next
Next

Func _ErrFunc()
    $HexNumber = Hex($oErrObj.number, 8)
    $aErrRet[0] = $HexNumber
    $aErrRet[1] = StringStripWS($oErrObj.description, 3)
    $aErrRet[2] = StringStripWS($oErrObj.windescription, 3)
    SetError(1); something to check for when this function returns
EndFunc
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0