Colyn1337 Posted June 1, 2012 Share Posted June 1, 2012 Hello Community! Before I submit a bug report, I wanted to see if maybe my problem is, well, me... Haha.. Anyways, I'm using an If/ElseIf/Else on a WMI Query to capture info. However, if the "If" or even the "ElseIf" statement is true, the "Else" is somehow true as well. My thinking was that the code was supposed to check the "If" and if it is false proceed thru the "ElseIf" statements until it found one to be true. Otherwise, it uses "Else" as the catchall. The problem I've run into, is that even tho the "If" or "ElseIf" is true, the code still runs the "Else". See code below... $objWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\HP\InstrumentedBIOS") If $objWMI = ("") Then Exit $colItems = $objWMI.ExecQuery("SELECT * FROM HPBIOS_BIOSString", "WQL", 0x20) If IsObj($colItems) Then For $objItems In $colItems If $objItems.Name = "Product Number" Then ; Newer laptops use this name $ProdNum = $objItems.Value ElseIf $objItems.Name = "SKU Number" Then ; Older laptops use this name $ProdNum = $objItems.Value Else $ProdNum = "Not Detectable" EndIf Next EndIf As a work around I've had to code it this way... $objWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\HP\InstrumentedBIOS") If $objWMI = ("") Then BasicSet() $colItems = $objWMI.ExecQuery("SELECT * FROM HPBIOS_BIOSString", "WQL", 0x20) If IsObj($colItems) Then For $objItems In $colItems If $objItems.Name = "Product Number" Then ; Newer laptops use this name $ProdNum = $objItems.Value ElseIf $objItems.Name = "SKU Number" Then ; Older laptops use this name $ProdNum = $objItems.Value EndIf If $ProdNum = "" Then $ProdNum = "Not Detectable" Next EndIf So, am I wrong in my thinking of how If/ElseIf/Else is supposed to work? Or is something else going on? Link to comment Share on other sites More sharing options...
jdelaney Posted June 1, 2012 Share Posted June 1, 2012 (edited) Put in some debugging, like a ConsoleWrite prior to the inner most If statement. something like this, possibly white spaces you aren't accounting for? ConsoleWrite ( "ObjectName= [" & $objItems.Name & "]...=? Product Number [" & ($objItems.Name = 'Product Number') & "]...=? SKU Number [" & ($objItems.Name = 'SKU Number') & "]." & @CRLF) Also, you are doing a loop, and returning ALL values of that loop to one variable, so that variable is in effect only being set by the last $objItems.Name. Edited June 1, 2012 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
water Posted June 1, 2012 Share Posted June 1, 2012 How about: If IsObj($colItems) Then For $objItems In $colItems If $objItems.Name = "Product Number" Or $objItems.Name = "SKU Number" Then ; Newer laptops use this name $ProdNum = $objItems.Value Else $ProdNum = "Not Detectable" EndIf Next EndIf My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 1, 2012 Moderators Share Posted June 1, 2012 Colyn1337, am I wrong in my thinking of how If/ElseIf/Else is supposed to work?No. Or is something else going on?Obviously, but I have no idea what. Is that code designed for any laptop - it looks as if it is for HP only which will limit your available testers. This amended code works fine for me though: ;$objWMI = ObjGet("winmgmts:" & @ComputerName & "rootHPInstrumentedBIOS") ;If $objWMI = ("") Then Exit ;$colItems = $objWMI.ExecQuery("SELECT * FROM HPBIOS_BIOSString", "WQL", 0x20) Global $colItems[3] = ["Product Number", "SKU Number", "fred"] ;If IsObj($colItems) Then For $objItems In $colItems If $objItems = "Product Number" Then ; Newer laptops use this name $ProdNum = "ProdN" ;$objItems.Value ElseIf $objItems = "SKU Number" Then ; Older laptops use this name $ProdNum = "SKUN" ;$objItems.Value Else $ProdNum = "Not Detectable" EndIf ConsoleWrite($ProdNum & @CRLF) Next ;EndIf M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
armoros Posted June 1, 2012 Share Posted June 1, 2012 I am on HP laptop but the code doesn't work.ProdNSKUNNot Detectable+>19:42:52 AutoIT3.exe ended.rc:0>Exit code: 0 Time: 2.105 [font="verdana, geneva, sans-serif"] [/font] Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 1, 2012 Moderators Share Posted June 1, 2012 armoros,My script can be run anywhere - it was just to show that the If...ElseIf...Else worked. Could you please run the code in the original post (adding the ConsoleWrite($ProdNum & @CRLF) line) and see what you get? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
jdelaney Posted June 1, 2012 Share Posted June 1, 2012 based on his output, the function ran exactly as you supplied it, Melba23 IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
armoros Posted June 1, 2012 Share Posted June 1, 2012 armoros,My script can be run anywhere - it was just to show that the If...ElseIf...Else worked. Could you please run the code in the original post (adding the ConsoleWrite($ProdNum & @CRLF) line) and see what you get? M23yes i added the lineConsoleWrite($ProdNum & @CRLF)It runs ok with no error output, but nothing else.. [font="verdana, geneva, sans-serif"] [/font] Link to comment Share on other sites More sharing options...
UEZ Posted June 1, 2012 Share Posted June 1, 2012 Try this: $objWMI = ObjGet("winmgmts:" & @ComputerName & "rootHPInstrumentedBIOS") $colItems = $objWMI.ExecQuery("SELECT * FROM HPBIOS_BIOSString", "WQL", 0x20) If IsObj($colItems) Then For $objItems In $colItems Switch $objItems.Name Case "Product Number", "SKU Number" $ProdNum = $objItems.Value Case Else $ProdNum = "Not Detectable" EndSwitch Next EndIf ConsoleWrite($ProdNum & @LF) 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Colyn1337 Posted June 1, 2012 Author Share Posted June 1, 2012 (edited) Oh wow...... I just figured out what's wrong..... and boy am I an idiot The variable $ProdNum is being overwritten because the if/elseif/else statement runs in a For/Next loop..... Whatever the last item in the query is wins on the control of that variable....... Which is never "Product Number" or "SKU Number"...#FacePalm($MyHead)EDIT 1:Thank you to everyone who replied...BTW, in our environment we have HP Probooks (6550, 6560, etc)... I'm not sure if they expose the bios in their home use products.EDIT 2:Figured out the solution.... ExitLoop (see below)$objWMI = ObjGet("winmgmts:" & @ComputerName & "rootHPInstrumentedBIOS") If $objWMI = ("") Then Exit $colItems = $objWMI.ExecQuery("SELECT * FROM HPBIOS_BIOSString", "wql", 0x20) If IsObj($colItems) Then For $objItems In $colItems If $objItems.Name = "Serial Number" Then $Serial = $objItems.Value ExitLoop ElseIf $objItems.Name = "Chassis Serial Number" Then $Serial = $objItems.Value ExitLoop Else $Serial = "not detectable." EndIf Next EndIf ConsoleWrite($Serial & @LF) Edited June 1, 2012 by Colyn1337 Link to comment Share on other sites More sharing options...
Colyn1337 Posted June 1, 2012 Author Share Posted June 1, 2012 (edited) Oooooo...... That's just...... Beautiful! EDIT: And it looked so promising... This code produces an end result of "Not Detectable"... Which is probably for the same reason my original code didn't work either...... Try this: $objWMI = ObjGet("winmgmts:" & @ComputerName & "rootHPInstrumentedBIOS") $colItems = $objWMI.ExecQuery("SELECT * FROM HPBIOS_BIOSString", "WQL", 0x20) If IsObj($colItems) Then For $objItems In $colItems Switch $objItems.Name Case "Product Number", "SKU Number" $ProdNum = $objItems.Value Case Else $ProdNum = "Not Detectable" EndSwitch Next EndIf ConsoleWrite($ProdNum & @LF) Br, UEZ Edited June 1, 2012 by Colyn1337 Link to comment Share on other sites More sharing options...
armoros Posted June 1, 2012 Share Posted June 1, 2012 This is mine output after UEZ's suggestion==> Variable must be of type "Object".:$colItems = $objWMI.ExecQuery("SELECT * FROM HPBIOS_BIOSString", "WQL", 0x20)$colItems = $objWMI^ ERROR->20:53:03 AutoIT3.exe ended.rc:1>Exit code: 1 Time: 2.160 [font="verdana, geneva, sans-serif"] [/font] Link to comment Share on other sites More sharing options...
Colyn1337 Posted June 1, 2012 Author Share Posted June 1, 2012 This is mine output after UEZ's suggestionYea, you'll get that if it couldn't find/create an object...... This is likely because you don't have the roothpinstrumentedbios namespace (not using an HP machine). Or, you didn't create/retrieve your object (e.g. $objWMI = ObjGet("winmgmts:" & @ComputerName & "rootHPInstrumentedBIOS") ) Link to comment Share on other sites More sharing options...
UEZ Posted June 1, 2012 Share Posted June 1, 2012 $objWMI = ObjGet("winmgmts:" & @ComputerName & "rootHPInstrumentedBIOS") has not error check and probably the WMI class rootHPInstrumentedBIOS doesn't exist on your machine! 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Colyn1337 Posted June 1, 2012 Author Share Posted June 1, 2012 $objWMI = ObjGet("winmgmts:" & @ComputerName & "rootHPInstrumentedBIOS") has not error check and probably the WMI class rootHPInstrumentedBIOS doesn't exist on your machine!Br,UEZHa, I had the same thought...... Tho, incase anyone missed it, see post #10 for the solution.... Link to comment Share on other sites More sharing options...
jdelaney Posted June 1, 2012 Share Posted June 1, 2012 Ahem, @post number 2 (pat self on back) IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now