Jump to content

COM error handling


 Share

Recommended Posts

On a vista system this code printed out

Quote

MsgBox(0, "", $aResult0 & "'s product state is indeterminate")

Will adding COM error handling correct it?

 

Local $aResult0, $aResult1, $ProductIsInOrOutOfDate, $ProductDisabledOrNot
$oWMI = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\localhost\root\SecurityCenter2")
$colItems = $oWMI.ExecQuery("Select * from AntiVirusProduct")
For $objAntiVirusProduct In $colItems
    $aResult0 = $objAntiVirusProduct.DisplayName
    $aResult1 = $objAntiVirusProduct.ProductState
    $ProductIsInOrOutOfDate = StringMid(Hex($aResult1), 7, 2)
    $ProductDisabledOrNot = StringMid(Hex($aResult1), 5, 2)
    If @error Then
        MsgBox(0, "", "Warning! *No AntiVirus Installed*" & @CRLF)
    Else
        Switch $ProductDisabledOrNot
            Case "10", "11"
                MsgBox(0, "", $aResult0 & " " & "(Enabled & " & (($ProductIsInOrOutOfDate = "10") ? "Not " : "") & "up-to-date)")
            Case "00", "01"
                MsgBox(0, "", $aResult0 & " " & "(Disabled & " & (($ProductIsInOrOutOfDate = "10") ? "Not " : "") & "up-to-date)")
            Case Else
                MsgBox(0, "", $aResult0 & "'s product state is indeterminate")
        EndSwitch
    EndIf
Next
If $aResult0 Then Exit
$colItems = $oWMI.ExecQuery("Select * from AntispywareProduct")
For $objAntiSpywareProduct In $colItems
    $aResult0 = $objAntiSpywareProduct.DisplayName
    $aResult1 = $objAntispywareProduct.ProductState
    $ProductIsInOrOutOfDate = StringMid(Hex($aResult1), 7, 2)
    $ProductDisabledOrNot = StringMid(Hex($aResult1), 5, 2)
    If @error Then
        MsgBox(0, "", "Warning! *No AntiVirus Installed*" & @CRLF)
    Else
        Switch $ProductDisabledOrNot
            Case "10", "11"
                MsgBox(0, "", $aResult0 & " " & "(Enabled & " & (($ProductIsInOrOutOfDate = "10") ? "Not " : "") & "up-to-date)")
            Case "00", "01"
                MsgBox(0, "", $aResult0 & " " & "(Disabled & " & (($ProductIsInOrOutOfDate = "10") ? "Not " : "") & "up-to-date)")
            Case Else
                MsgBox(0, "", $aResult0 & "'s product state is indeterminate")
        EndSwitch
    EndIf
Next

 

Link to comment
Share on other sites

I don't have a Vista machine, but I will attempt to help...

1. You should always use a COM Error Handler when you work with objects.

2. If you are getting other information like DisplayName - then no, it won't help with other results.

3. What is the RAW data from DisplayName and ProductState ?

4. You may need to insert #RequireAdmin at the very top of your script.

Give me a hour or so and I'll rework your script with error handling.

 

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Not tested, but should work. Please read the notes in the script.

#RequireAdmin
Opt('MustDeclareVars', 1)
;
Local $_objError = ObjEvent('AutoIt.Error', '_ObjErrorHandler')
;
Local $sReturn = _SC2_AntiVirus()
Local $nError = @error
If $nError Then
    MsgBox(0, 'Error: ' & $nError, $sReturn)
Else
    MsgBox(0, 'Results', $sReturn)
EndIf
;
Func _SC2_AntiVirus()
    Local $objWMI = ObjGet('Winmgmts:{ImpersonationLevel=Impersonate,AuthenticationLevel=PktPrivacy,(Debug,Security)}!\\localhost\root\SecurityCenter2')
    Local $colItems = $objWMI.ExecQuery('Select * from AntiVirusProduct')
    Local $NumberOfObjects = $colItems.Count
    If @error Or $NumberOfObjects = 0 Then
        Return SetError(-1, 0, 'Object Not Found - No Data Available'); <-- This will tell you if AV is not installed
    EndIf
    ;
    Local $sDisplayName, $nProductState, $ProductIsInOrOutOfDate, $ProductDisabledOrNot, $str = ''
    ;
    For $objAntiVirusProduct In $colItems
        $sDisplayName = $objAntiVirusProduct.DisplayName
        $nProductState = $objAntiVirusProduct.ProductState
        $ProductIsInOrOutOfDate = StringMid(Hex($nProductState), 7, 2)
        $ProductDisabledOrNot = StringMid(Hex($nProductState), 5, 2)
        Switch $ProductDisabledOrNot
            Case '10', '11'
                $str &= $sDisplayName & " (Enabled & " & (($ProductIsInOrOutOfDate = "10") ? "Not " : "") & "up-to-date)"
            Case '00', '01'
                $str &= $sDisplayName & " (Disabled & " & (($ProductIsInOrOutOfDate = "10") ? "Not " : "") & "up-to-date)"
            Case Else
                $str &= $sDisplayName & "'s product state is indeterminate"
        EndSwitch
        $str &= @CRLF
    Next
    Return $str
EndFunc
;
; Minimal COM Error Handler
Func _ObjErrorHandler($_objError)
    $_objError.Clear
    Return SetError(1)
EndFunc
;

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Thanks ripdad. I also don't have access to a Vista system however, I ran it on my win 10 64X and it printed out win defender but not Avast which is installed and UTD.

I'll keep working on it.

Edited by Bettylou
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...