VBScript从WMI查询循环中获取值的总和

时间:2018-05-23 03:53:21

标签: vbscript wmi

下面显示的基本脚本将回显它在colItems中找到的每个objItem.MemoryDe​​vices(M)的值。但是,这不是我想要的。由于每个值总是一个数字,我希望它能回显它找到的所有值的总和。

strComputer = "."

Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMemoryArray",,48 )


For Each objItem in colItems
    M = objItem.MemoryDevices
    Wscript.Echo M
Next

1 个答案:

答案 0 :(得分:1)

VBScript将自动在字符串和数字类型之间进行转换。您应该能够通过将总计集添加到数字来完成您想要的任务,然后通过变量M增加总计。

strComputer = "."

Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set colItems = objWMIService.ExecQuery("SELECT * FROM 
Win32_PhysicalMemoryArray",,48 )

Total = 0
For Each objItem in colItems
    M = objItem.MemoryDevices
    Total = Total + M
Next

WScript.Echo Total