Chris
Chris

Reputation: 261

VBScript get sum of values from WMI query loop

The basic script shown below will echo the value of each objItem.MemoryDevices (M) it finds in colItems. However, that's not what I want. Since each value will always be a number, I want it to echo the total sum of all the values it finds.

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

Upvotes: 0

Views: 358

Answers (1)

Markus
Markus

Reputation: 761

VBScript will automatically convert between string and numerical types. You should be able to accomplish what you want by adding a Total set to a number, then increment the Total by your variable 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

Upvotes: 1

Related Questions