将数组中的属性字符串解析为For Each循环

时间:2019-05-28 09:05:31

标签: vbscript

我正在尝试使用VBScript获取有关笔记本电脑电池的信息。

更多属性。我想为每个arrayItems("Name","Availability","BatteryStatus","Chemistry")解析objItem

例如:

For iii = 0 To UBound(arrayItems)
    WScript.Echo "Result of iii:" & objItem.arrayItems (iii)
Next

我不想手动输入"Availability: " & objItem.Availability"BatteryStatus: " & objItem.BatteryStatus,...

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Battery",,48)
For Each objItem In colItems
    WScript.Echo "Name: " & objItem.Name
    WScript.Echo "Availability: " & objItem.Availability
    WScript.Echo "BatteryStatus: " & objItem.BatteryStatus
    WScript.Echo "Chemistry: " & objItem.Chemistry
    WScript.Echo "DesignVoltage: " & objItem.DesignVoltage
    WScript.Echo "EstimatedChargeRemaining: " & objItem.EstimatedChargeRemaining
    WScript.Echo "Status: " & objItem.Status
Next

1 个答案:

答案 0 :(得分:2)

您可以通过对象的properties_ property set完成以下操作:

arrayItems = Array("Name", "Availability", "BatteryStatus", "Chemistry")
For Each objItem In colItems
    For Each name In arrayItems
        WScript.Echo name & ": " & objItem.properties_(name)
    Next
Next