WMI:如何确定活动实例的InstanceName?

时间:2016-06-21 17:09:54

标签: vba vbscript wmi

在处理WMI时,我仍然有点不稳定,但这就是场景。我的公司在整个域中部署了几种型号的惠普笔记本电脑,我们需要更改BIOS中的资产标签设置。此代码适用于我们所有的HP EliteBook:

Set objWMIService = objSWbemLocator.ConnectServer( _ 
    strComputer, "root\HP\InstrumentedBIOS")

'We have to search for the exact bios tag name because it varies by model
Set colItems = objWMIService.ExecQuery("SELECT * FROM HPBIOS_BIOSString", , 48)
For Each objItem In colItems
    If InStr(1, objItem.Name, "Asset") Then strName = objItem.Name
Next

' Obtain an instance of the the class using a key property value.
Set objShare = objWMIService.Get( _ 
    "HPBIOS_BIOSSettingInterface.InstanceName='ACPI\PNP0C14\0_0'")

' Obtain an InParameters object specific to the method.
Set objInParam = objShare.Methods("SetBIOSSetting").InParameters.SpawnInstance_()

' Add the input parameters.
objInParam.Properties_.item("Name") = strName
objInParam.Properties_.item("Value") = strAssetTag
objInParam.Properties_.item("Password") = "<utf-16/>" & strPassword

' Execute the method and obtain the return status.
' The OutParameters object in objOutParams is created by the provider.
Set objOutParams = objWMIService.ExecMethod( _
    "HPBIOS_BIOSSettingInterface.InstanceName='ACPI\PNP0C14\0_0'", _
    "SetBIOSSetting", objInParam)

Select Case objOutParams.return
    Case 0
        strReturn = "Success. Asset Tag " & strAssetTag & _
                    " will be configured the next time you reboot " & _
                    strComputer & "."
    Case 1
        strReturn = "1: Not Supported"
    Case 2
        strReturn = "2: Unspecified Error"
    Case 3
        strReturn = "3: Timeout"
    Case 4
        strReturn = "4: Failed"
    Case 5
        strReturn = "5: Invalid Parameter"
    Case 6
        strReturn = "6: Access Denied"
    Case Else
        strReturn = "..."
End Select

问题是在HP ZBooks上,InstaneName是ACPI\PNP0C14\1_0。因此,将行更改为正确的实例使其适用于ZBooks,但为EliteBooks打破了它。例如:

Set objShare = objWMIService.Get( _ 
    "HPBIOS_BIOSSettingInterface.InstanceName='ACPI\PNP0C14\1_0'")

我可以创建两个脚本,一个用于EliteBooks,一个用于ZBooks,但由于总是只有一个Active实例,我宁愿创建一个获取该活动实例的脚本。例如(我希望工作的非工作代码):

Set objShare = objWMIService.Get( _ 
    "HPBIOS_BIOSSettingInterface.Active='True'")

那么,我怎样才能找到有效InstanceName的{​​{1}}?

1 个答案:

答案 0 :(得分:0)

所以也许这太简单了,或者可能有更好的方法来做到这一点,但这里是我最终解决它的方法:

我只是重用了colItems对象来获取Instances的集合。应该只有一个,但我通过检查它的.Active属性验证我有正确的实例。

'We have to search for the exact bios tag name because it varies by model
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM HPBIOS_BIOSString", , 48)
For Each objItem In colItems
    If InStr(1, objItem.Name, "Asset") Then strTagName = objItem.Name
Next

'We have to search for the exact instance name because it varies by model
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM HPBIOS_BIOSSettingInterface", , 48)
For Each objItem In colItems
    If objItem.Active = "True" Then
        strInstanceName = objItem.InstanceName
    End If
Next

' Obtain the active instance.
Set objShare = objWMIService.Get( _
    "HPBIOS_BIOSSettingInterface.InstanceName='" & strInstanceName & "'")

不确定这是否是最干净的方法,但它确实有效。