Sign in to follow this  
Followers 0
bourny

Help with WMI and objects

9 posts in this topic

I am running a script to query WMI to pull out if a system is 32 bit or not. This works fine but is tripped up when it cannot pull a value and errors out. The error I get in SciTE is:

$Type = $objOperatingSystem.OSArchitecture

$Type = $objOperatingSystem.OSArchitecture^ ERROR

The issue is I am not handling correctly if $objWMIService = ObjGet if an object has not been returned. I have tried if not isobj( $objWMIService) but this does not work .

My script is as follows:

$type = ""
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$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)
For $objOperatingSystem in $colItems
  $Type = $objOperatingSystem.OSArchitecture
  ;Msgbox(0,"",$objOperatingSystem)
Next

Msgbox(0,"",$type)

Any help appreciated.

Share this post


Link to post
Share on other sites



You need a COM error handler. Please see function ObjEvent in the help file for an example.

; COM Error Handler example
; -------------------------
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; Initialize a COM error handler
; ... Your code goes here
Exit
; This is my custom defined error handler
Func MyErrFunc()
  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"    & @CRLF  & @CRLF & _
    "err.description is: "  & @TAB & $oMyError.description  & @CRLF & _
    "err.windescription:"    & @TAB & $oMyError.windescription & @CRLF & _
    "err.number is: "        & @TAB & hex($oMyError.number,8)  & @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 _
            )
Endfunc


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks,

Got that working now. I have done the script below to overcome this issue. I am assuming from this the OBJEvent is like an adlib that is constantly running and only deals with objget errors and nothing else. Why do you need to use "If not ISobj($value) if you have the OBJEvent handler. I am not that advanced in scripting with objects hence my lack of understanding.

My new code :

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; Initialize a COM error handler
$type = ""
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
  For $objOperatingSystem in $colItems
   $Type = $objOperatingSystem.OSArchitecture
   ;Msgbox(0,"",$objOperatingSystem)
  Next
  If $Type = "" Then
   Msgbox(0,"Nothing found", "")
  Else
   Msgbox(0,"Found this value", $Type)
  EndIf
Func MyErrFunc()

Endfunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi, bourny. Is this part of a larger script? If, as it appears from your original post, you're only trying to find the architecture, using @OSArch would be a lot simpler.

MsgBox(0, "", @OSArch)
Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Yes it is and it would be easier to do this but I am forcing a trainee to use WMI to extract information on various areas of the system. This brought up the issue of systems not having this information available on the WMI class so it needed to handle this correctly instead of falling over with autoit errors. As you have correctly stated this needs a com error handler set.

Can I still request an answer to my follow on question please.:

q. I am assuming from this the OBJEvent is like an adlib that is constantly running and only deals with objget errors and nothing else.

Share this post


Link to post
Share on other sites

I am assuming from this the OBJEvent is like an adlib that is constantly running and only deals with objget errors and nothing else.

AdlLib is called every x milliseconds (you specify x with AdlibRegister) and then runs the function you specified.

The COM error handler you defined with ObjEvent is only called when an error occurres. The COM error handler not only handles ObjGet errors but ALL COM related errors. That means: If you call a method that doesn't exist, pass the wrong number of parameters or of the wrong type, try to access none existing properties of an object etc. etc.

Why do you need to use "If not ISobj($value) if you have the OBJEvent handler.

Just in case a method returns an object when everything is fine and 0 or whatever when there was an error. In most cases the COM error handler is enough.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Water: Thanks for your reply.

Since using the "$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")" function this has fixed most issues apart from one machine. I have an XP machine out of a list of 10 machines that keeps chucking up a different error only when the OBJEvent handler is used. The ScIte error I get is as follows

>Running:(3.3.6.1):C:Program FilesAutoIt3autoit3.exe "C:Program FilesAutoIt3TEST1.au3"

C:Program FilesAutoIt3TEST1.au3 (18) : ==> Missing right bracket ')' in expression.:

$colItems = $objWMIService.ExecQuery("Select * from Win32_ComputerSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

$colItems = ^ ERROR

->10:05:13 AutoIT3.exe ended.rc:1

>Exit code: 1 Time: 22.782

If I then add an "If not isOBJ after the "$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")" This then fixes that issue.

This tells me I need to not only use the "if not isobj" but also the "$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")",

My question is to understand the differences. Why does above quotes error with bracket occur if the "If not isobj" present. I thought the OBJevent woulkd handle all errors. Can you explain the difference to allow me to understand.

Share this post


Link to post
Share on other sites

You are getting a syntax error. Press Ctrl + F5 in SciTe and you should get this error.

If you use the same code on all machines you should get this error on every machine.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Try this:

Global $oErrorHandler = ObjEvent("AutoIt.Error", "ObjErrorHandler")

$type = ""
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
If @error Then Exit MsgBox(16, "Error", "Error")
$colItems = $objWMIService.ExecQuery("Select * from Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
    For $objOperatingSystem in $colItems
      $Type = $objOperatingSystem.OSArchitecture
      ;Msgbox(0,"",$objOperatingSystem)
    Next

    Msgbox(0,"",$type)
Else
    MsgBox(16, "Error", "Error")
EndIf

Func ObjErrorHandler()
     ConsoleWrite(  "A COM Error has occured!" & @CRLF  & @CRLF & _
                                "err.description is: "    & @TAB & $oErrorHandler.description    & @CRLF & _
                                "err.windescription:"     & @TAB & $oErrorHandler & @CRLF & _
                                "err.number is: "         & @TAB & Hex($oErrorHandler.number, 8)  & @CRLF & _
                                "err.lastdllerror is: "   & @TAB & $oErrorHandler.lastdllerror   & @CRLF & _
                                "err.scriptline is: "     & @TAB & $oErrorHandler.scriptline     & @CRLF & _
                                "err.source is: "         & @TAB & $oErrorHandler.source         & @CRLF & _
                                "err.helpfile is: "       & @TAB & $oErrorHandler.helpfile       & @CRLF & _
                                "err.helpcontext is: "    & @TAB & $oErrorHandler.helpcontext & @CRLF _
                            )
EndFunc

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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