Jump to content
Sign in to follow this  
JLogan3o13

Trouble catching a WMI error

Recommended Posts

JLogan3o13

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
BrewManNH

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
JLogan3o13

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
BrewManNH

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
JLogan3o13

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  

  • Similar Content

    • AzgarD
      By AzgarD
      Hi guys. I know this is a newbie topic, very newbie, but i've read a lot of stuff and still don't get it. I just need to copy something from Excel cell, paste this in other program, copy something in this program and paste in other Excel cell. Something like...
      Copy A2 Use some WindowActivate and MouseMove stuff and CTRL+C (not a problem) Go back to the Excel sheet Paste that content in C2 Then Copy A3 Use some WindowActivate and MouseMove stuff and CTRL+C (not a problem) Go back to the Excel sheet Paste that content in C3 ... And it goes on The problem is, how can i "communicate" with Excel and do this row change? Like A2 to C2 and A3 to C3 ... In a efficient way that can be done like hundreds of times.
      Very newbie question but still not understanding this.
       
      Ty guys.
    • Gowrisankar
      By Gowrisankar
      Dear members of the forum,
      I need to open excel files that may or may not need a password and finally move the files that needs password to manual queue.
      Is there a fastest way to do this?
       
      PS: I have a huge respect for the rules of this forum. I am not asking assistance to override any security measure. I just need to segregate the files that needs passwords.
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I'm working on a little project of mines, and I was trying to use WMI Object.
      The question which I don't find an answer is: 
      Once I do the query with WMI Object, something like "SELECT * FROM Win32_LogonSession", instead of specify the field of the collection returned, ( i.e. $colItems.Caption ), can I loop though each property and each value of the property, writing so one row of code only?
      Hope my question was clear enough.
      Thanks in advance.

      Best Regards.
    • MrCheese
      By MrCheese
      Hi guys,
      without including everything (unless you want it)
      I am copying data from a table in chrome and wanting to paste it into excel.
      Copying in Chrome works.
      I can paste it into the field i want by emulating goto -> ctrl V:
      WinActivate($dataload) WinWaitActive($dataload) Sleep(500) $oWorkbook1.Sheets("ItemReturn").Activate Sleep(500) $msg = "Measuring Sheet" conwrite() ttips2() Local Const $xlUp = -4162 With $oWorkbook1.ActiveSheet ; process active sheet $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) ; get a Range that contains the last used cells $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, "B")).Rows.Count ; get the the row count for the range starting in row/column 1 and ending at the last used row/column $iLastCell = .Cells($iRowCount + 1, "B").End($xlUp).Row ; start in the row following the last used row and move up to the first used cell in column "B" and grab this row number EndWith $NewStartCell = $iLastCell + 2 $msg = "moving to location" conwrite() ttips2() Sleep(250) Send("^g") WinWait("Go To") Sleep(100) Send("B" & $NewStartCell) Sleep(100) Send("{ENTER}") Sleep(500) Send("^v")  
      But, I want to use _excel_rangecopypaste, pasting from the clipboard
      _Excel_RangeCopyPaste($oWorkbook1.ActiveSheet, default, "B" & $NewStartCell,default,$xlPasteValuesAndNumberFormats) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeCopy Example 2", "Error pasting cells." & @CRLF & "@error = " & @error & ", @extended = " & @extended) however, this gives me error 4 , extended@:  -2147352567
      How can i fix this or find out how to debug this error?
       
      Thanks
    • Simpel
      By Simpel
      Hi.
      I try to figure out who is using a excel workbook which I can only open "read only". I use this code:
      #include <Array.au3> #include <Excel.au3> Local $sFile = ; excel file with path on a network drive Local $oExcel = _Excel_Open(True, True) Local $oTabelle = _Excel_BookOpen($oExcel, $sFile) Local $aUsers If IsObj($oTabelle) Then $aUsers = $oTabelle.UserStatus _ArrayDisplay($aUsers) EndIf If I am the one allowed to write to the excel file (I'm the first one who opened it) then I will get an array with myself:

      If my collegue opened the excel file first and I run the code I get the following error message:
      "H:\_Conrad lokal\Downloads\AutoIt3\_COX\Tests\test.au3" (9) : ==> The requested action with this object has failed.: $aUsers = $oTabelle.UserStatus $aUsers = $oTabelle^ ERROR The excel file is on a network drive. Is that's the problem?
      Regards, Conrad
×