Jump to content

Trouble catching a WMI error


JLogan3o13
 Share

Recommended Posts

  • Moderators

I am working on a small script for a customer, and am stuck the last line item. I have been looking at it too long, as I'm sure I am missing something obvious. If anyone could offer suggestions, I would appreciate it.

This customer is a health care facility. They have about 3000 carts in patients' rooms, connected to a thin client device. The carts are supposed to have functionality built in that keeps track of battery level should they be unplugged, and shuts the machine down gracefully if it gets below a threshold. Unfortunately, this functionality is not working, so until it can be fixed by the vendor, they need another way to do it.

The script reads an excel workbook, by sheet, into an array. It then runs the Battery function against each item in the array, and outputs to a second workbook the results (screenshot), either an error if it could not connect or the % of battery remaining if it can. This all works just fine.

The problem is with a small subset of machines that are pingable and on the network, but have issues with WMI. Even though I can ping them, I cannot connect to the namespace on those machines. In this case, nothing is returned in the second workbook but a blank line. I've tried putting an error handler in, but as the script continues on, this doesn't seem to work. I also tried adding another statement to check if the machine is pingable, at the commented line in the script below, but this doesn't work either. I can't figure how to catch this issue.

post-54985-0-41180700-1347676233_thumb.p

#include <Array.au3>
#include <Excel.au3>
#include <File.au3>
Global $wbemFlagReturnImmediately = "&h10"
Global $wbemFlagForwardOnly = "&h20"
Local $oExcel1 = _ExcelBookNew(1)
Local $oExcel2 = _ExcelBookOpen(@DesktopDir & "\Carts.xlsx", 0, True)
Local $aArray[31] = ["100005001", "100013013", "100013018", "100013019", "100013020", "100013021", "100013022", "100013023", "100013024", _
     "100013025", "100013026", "100013027", "100013028", "100013030", "100013031", "100013032", "100013033", _
     "100013037", "100013039", "100013040", "100013042", "100013062", "100013080", "100013086", "100013092", _
     "100013102", "100013103", "100013113", "100013136", "100013158", "Other"]

_setup($oExcel1, $aArray)

For $i = 1 To 31
     Local $aSheetArray = _ExcelReadSheetToArray($oExcel2, 2, 2, 0, 1)
         For $x = 1 To $aSheetArray[0][0]
             Global $objWMIService = ObjGet("winmgmts:\\" & $aSheetArray[$x][1] & "\root\CIMV2")
             If @error Then ;<====================here================================
                 _ExcelWriteCell($oExcel1, $aSheetArray[$x][1], $x, 1)
                 _ExcelWriteCell($oExcel1, "Could not connect to asset " & $aSheetArray[$x][1] & "via WMI.", $x, 2)
             Else
                 _Battery($oExcel1, $aSheetArray)
             EndIf
         Next
     _ExcelSheetActivate($oExcel1, $aArray[$i])
     _ExcelSheetActivate($oExcel2, $aArray[$i])
Next

_ExcelBookClose($oExcel2)
_ExcelBookSave($oExcel1)
_ExcelBookClose($oExcel1)

Func _setup($oExcel1, $aArray)

_ExcelSheetDelete($oExcel1, "Sheet2")
_ExcelSheetDelete($oExcel1, "Sheet3")
_ExcelSheetNameSet($oExcel1, "100005001")

For $i = 1 To Ubound($aArray) - 1
     _ExcelSheetAddNew($oExcel1, $aArray[$i])
     _ExcelSheetMove($oExcel1, $aArray[$i], $i + 1, False)
Next

_ExcelSheetMove($oExcel1, "100005001", 1, True)
_ExcelBookSaveAs($oExcel1, @DesktopDir & "\Cart Report " & @MON & "_" & @MDAY & "_" & @HOUR & "_" & @MIN & "_" & @SEC, "xls")

EndFunc

Func _Battery($oExcel1, $aSheetArray)

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Battery", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If @error Then
     _ExcelWriteCell($oExcel1, $aSheetArray[$x][1], $x, 1)
     _ExcelWriteCell($oExcel1, "Error connecting to WMI namespace.", $x, 2)
Else
     For $objItem In $colItems
         If $objItem.EstimatedChargeRemaining < "90" Then
         _ExcelWriteCell($oExcel1, $aSheetArray[$x][1], $x, 1)
         _ExcelWriteCell($oExcel1, "Charge Remaining is below threshhold at " & $objItem.EstimatedChargeRemaining & "%", $x, 2)
         Else
         _ExcelWriteCell($oExcel1, $aSheetArray[$x][1], $x, 1)
         _ExcelWriteCell($oExcel1, "Charge Remaining is " & $objItem.EstimatedChargeRemaining & "%", $x, 2)
         EndIf
     Next
EndIf

EndFunc

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

So are you saying that when they can't connect using WMI that @error isn't giving you any indication of it?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

When it attempts to connect via WMI and the machine is offline, it returns the error as it should. However, if the machine is pingable, but I cannot connect to WMI on it, it returns a blank line. This customer uses SCCM and I run into the same issue on these machines, I can ping them, but if I attempt to connect to WMI through the SCCM tools, I get an error that it "could not connect to WMI namespace".

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

In that case, when it doesn't connect to the namespace, check to see if $objWMIService is an object (using IsObj) and see what happens.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

Thanks for the tip, BrewManNH. I thought that might do it, but tested this morning and still get just a blank line. I am going to do some testing again today, and see if I can find a way around it. Otherwise, I can always correlate the array index to the blank line in another function. It's ugly, but it would work. Thanks again.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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

×
×
  • Create New...