Jump to content
Sign in to follow this  
Colyn1337

User Error or Bug w/ If/ElseIf/Else

Recommended Posts

Colyn1337

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?

Share this post


Link to post
Share on other sites
jdelaney

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

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
Melba23

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
armoros

I am on HP laptop but the code doesn't work.

ProdN

SKUN

Not Detectable

+>19:42:52 AutoIT3.exe ended.rc:0

>Exit code: 0 Time: 2.105


[font="verdana, geneva, sans-serif"] [/font]

Share this post


Link to post
Share on other sites
Melba23

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
armoros

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

yes i added the line

ConsoleWrite($ProdNum & @CRLF)

It runs ok with no error output, but nothing else..


[font="verdana, geneva, sans-serif"] [/font]

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Colyn1337

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 by Colyn1337

Share this post


Link to post
Share on other sites
Colyn1337

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 by Colyn1337

Share this post


Link to post
Share on other sites
armoros

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]

Share this post


Link to post
Share on other sites
Colyn1337

This is mine output after UEZ's suggestion

Yea, 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") )

Share this post


Link to post
Share on other sites
UEZ

$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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Colyn1337

$objWMI = ObjGet("winmgmts:" & @ComputerName & "rootHPInstrumentedBIOS") has not error check and probably the WMI class rootHPInstrumentedBIOS doesn't exist on your machine!

Br,

UEZ

Ha, I had the same thought...... Tho, incase anyone missed it, see post #10 for the solution....

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  

×