Sign in to follow this  
Followers 0
Obviator

WMI Scripting Issues

4 posts in this topic

i've been puzzling over this for a while now and can't seem to find a reasonable solution.

i have tw functions that use WMI to get system manufacturer and battery information. As two separate functions they return what i expect. When i try to merge them into one function by increasing the passed variables i get errors that i can't really explain.

i'll use the system manufacturer function as an example:

$mMfg = _QueryMfg($mMfg)

Func _QueryMfg($mMfg)
Dim $objWMIService, $colItems, $Output
$colItems = ""
$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL")
If IsObj($colItems) Then
     For $objItem in $colItems
         $Output = $Output & "Manufacturer: " & $objItem.Manufacturer & @CRLF
         $mMfg = $objItem.Manufacturer
         $Output = ""
     Next
Else
     MsgBox(0, "WMI Output", "No WMI objects found for class Win32_BIOS")
Endif
Return($mMfg)
EndFunc

When i add passed variables for properties ($Properties = "Manufacturer") and path ($Path = "Win32_BIOS") i start getting "error in expression".

$mMfg = _WMIQuery($Item, "Manufacturer", "Win32_BIOS")

Func _WMIQuery($Item, $Properties, $Path)
Dim $objWMIService, $colItems, $Output
$colItems = ""
$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM " & $Path, "WQL")
If IsObj($colItems) Then
     For $objItem in $colItems
         $Output = $Output & $Properties ": " & $objItem.$Properties & @CRLF ;This line gets "Error in Expression" If i remark out this line...
         $Item = $objItem.$Properties        ;...This line gets "Error in Expression"
         $Output = ""
     Next
Else
     MsgBox(0, "WMI Output", "No WMI objects found for class " & $Path)
Endif
Return($Item)
EndFunc

i can't seem to wrap my head around what i may be doing wrong. Anyone have any ideas?

Thanks,

Dave

Share this post


Link to post
Share on other sites



For this:

$Output = $Output & $Properties ": " & $objItem.$Properties & @CRLF ;This line gets "Error in Expression" If i remark out this line...

You need an & after $Properties and before ": "

And for the $objItem.$Properties, try like this:

Execute("$objItem." & $Properties)

Final line would look something like:

$Output = $Output & $Properties & ": " & Execute("$objItem." & $Properties) & @CRLF

Share this post


Link to post
Share on other sites

Here's another way...

Local $sRtn = _WMI_SingleQuery("Win32_BIOS", "Manufacturer")

If @error Then
    MsgBox(0, "Error", $sRtn); do something with error
Else
    MsgBox(0, "Result", $sRtn); <-- returned value
EndIf

Func _WMI_SingleQuery($sClass, $sProperty)
    Local $objWMI = ObjGet("winmgmts:" & @ComputerName & "rootCIMV2")
    Local $objInstances = $objWMI.InstancesOf($sClass)
    Local $count = $objInstances.Count
    If @error Or Not $count Then
        Return SetError(-1, 0, "Object Error")
    EndIf

    For $objItem In $objInstances
        For $objProperties In $objItem.Properties_()
            If ($objProperties.Name = $sProperty) Then
                Return $objProperties.Value
            EndIf
        Next
    Next
    Return SetError(-2, 0, "Nothing Found")
EndFunc

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks MrMitchell! i played with all kinds of permutations of quotation marks, except this one. Never even dawned on me. Thanks for catching the "&" also, i sometimes so wrapped up in the details i forget things.

Dave

Edited by Obviator

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