将For Each循环中的属性值连接到一行输出

时间:2019-05-30 01:27:02

标签: vbscript wmi wmic

对不起,我的英语不好。

Parse properties string in an array to For Each loophttps://stackoverflow.com/users/1630171/的支持下 我能够读取电池信息。但这只是单独的信息,所以我们如何组合起来并将结果导出到一行。

<canvas id="canvas"></canvas>

它显示输出为空结果。

1 个答案:

答案 0 :(得分:1)

您需要收集属性并将它们连接成字符串。

尝试一下:

Option Explicit

Dim objItem, arrayItems, strComputer
Dim objWMIService, colItems, arrResult, i

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

'The third parameter 48 for 'ExecQuery' is the combination of wbemFlagForwardOnly + wbemFlagReturnImmediately
'see: https://docs.microsoft.com/en-us/windows/desktop/wmisdk/swbemservices-execquery#parameters
Set colItems = objWMIService.ExecQuery("Select * from Win32_Battery",,48)
arrayItems = Array("Name", "Availability", "BatteryStatus", "Chemistry")

For Each objItem in colItems
    'create/clear an array to store the various pieces of information
    ReDim arrResult(UBound(arrayItems))
    For i = 0 To (UBound(arrayItems))
        ' Sometimes the WMI property returns a Null value (Nothing)..
        If Not IsNull (objItem.Properties_(arrayItems(i))) Then
            arrResult(i) = objItem.Properties_(arrayItems(i))
        Else
            arrResult(i) = "Unknown"
        End If
    Next
    'as example I'm using the Tab character to join the pieces
    WScript.Echo Join(arrResult, Chr(9))
Next