Sign in to follow this  
Followers 0
JLogan3o13

Trouble catching a WMI error

5 posts in this topic

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

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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".


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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.


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

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

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to store an entire row of a Excel workbook into an array.  The row index is stored in a variable.
      How can i do it?
      Thanks in advance for your support.
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to write a value into an excel column.
      I know where it starts from, but i don't know what the end is, last non-empty cell.
      How can i get the number of last non-empty cell?
      Thanks in advance.
      Regards 
    • Nareshm
      By Nareshm
      Hi All,
      I have excel file like this
      and i want to cut cell/text from excel to other software.

       
      I have to cut the cell of B column one by one and past into other software
      If Winexists("No Data Found")
      then restore cuted cell and goto next/down side cell
      How to do it ?
    • water
      By water
      Extensive library to control and manipulate Microsoft Excel charts.
      Theads: General Help & Support - Example Scripts
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2017-07-21)
      None. The COM error handling related bugs have been fixed.
       
    • Mag91
      By Mag91
      Hey Community,
      cause im too new in the Auto it world i will try it with the your help. hopefully.
      I woud like to know how i can handle my Problem.
      ----
      I have a Excel Data with 362 random numbers.
      For Example:
      1166642335374 1172899897343
      .....
      this numbers are a part of the filepath ...example
      D:\Projekte\1166_64233_5374
      as u can see its the first number of the Excel data. After the first 4 numbers it shoud make a "_" than another 5 "_"
      This is my first question. How can i handle this to make it Shell execute.
       
      --------
      Second question:
      If i am in the path.
      For Example:
      D:\Projekte\1166_64233_5374
      the code shoud search for specific PDF Files.
      They are named like: 0050569E364B1ED79B900F73E62660EC.pdf
      the first 15 letters are always the same
      0050569E364B1ED
      when he found this data he has to copy it on a Folder on the Desktop.
      (There can also be 2 or 3 pdfs in one Folder with this letters)
      ----
      Please give me some help :-)