Jump to content

User Error or Bug w/ If/ElseIf/Else


Recommended Posts

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

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.
Link to comment
Share on other sites

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 - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

$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

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...