WMI Scripts

WMI Memory
How Much RAM Is In the Computer?

Introduction to WMI Memory

This page explains how to create a Microsoft WMI script.  The examples interrogate the Windows Servers and report on how much RAM is installed.  My WMI code employs Win32 ComputerSystem commands to interrogate the operating system and report TotalPhysicalMemory (RAM).  The beauty of learning this Win32 method is that you could adapt it to other hardware properties, for example, a disk's free space or a fan's voltage.

Topics for WMI Memory Script

 ♦

Scenario - When to use this WMI Memory Script

You may be surprised that some of your machines may not have the amount of RAM that you thought.  Perhaps a SIMM chip has worked loose on a Windows Server?  Worse, perhaps that Psycho user took the back off his desktop machine and substituted a smaller RAM stick for the original.  On the other hand, you may get a pleasant surprise and discover more memory than you expected.

Example 1 - Discover how much RAM there is on "NetworkMachine"

Let us write a VBScript to find out how much TotalPhysicalMemory (RAM) there is in a particular Windows Server.  What we need to do is query a CIM (Common Information Model) class called Win32_ComputerSystem and ask it to display TotalPhysicalMemory.  The first tutorial example covers the GetObject and ExecQuery, then the second example script adds flexibility with an InputBox.

This sample script will query the operating system and then echo the TotalPhysicalMemory to your screen.

Prerequisites for your WMI Script

No specific requirements.  I cannot think of a current Microsoft operating system that does not have the Win32_ComputerSystem Class.

Instructions for Creating your TotalPhysicalMemory WMI Script

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Decide which machine to interrogate and then change line 10 accordingly:
    my default for strComputer = "." means the current machine.
  3. Save the file with a .vbs extension, for example: Memory.vbs 
  4. Double click Memory.vbs and check the TotalPhysicalMemory (RAM).

Script to Discover how much RAM there is on "NetworkMachine"

 ' Memory.vbs
' Sample VBScript to discover how much RAM in computer
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - August 2010
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strComputer

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
Wscript.Echo "System Name: " & objComputer.Name _
& vbCr & "Total RAM " & objComputer.TotalPhysicalMemory
Next

WScript.Quit

' End of free example of Memory WMI / VBScript

 

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

WMI Tutorial - Learning Points

From a WMI perspective

1) Let us connect to the Root of the CIM namespace.   Here is the first procedure, as with all WMI scripts we instruct winmgmts to access the root of the CIM library :
Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")

2) In this instance we need security clearance to query the machine's hardware, this is why we add :
 & "{impersonationLevel=impersonate}!\\" _  

Fact: there is a registry setting: HKLM\SOFTWARE\Microsoft\WBEM\Scripting where the Default Impersonation Level is set to 3.

Guy's speculation is that when you use WMI to interrogate the client machine, there are 4 levels of impersonation, anonymous, identify, impersonate, and delegate.  Each successive level giving more privileges to the script on the client machine.  In this example, Level 3 = impersonate.

Experimentation.  The script worked just the same if I merely set the above WBEM registry key to 1, 2, 3, or 4.  However, the script failed when I substituted delegate, "{impersonationLevel=delegate}!\\" _

3) Set colComputer = objWMIService.ExecQuery _  This is a standard VBScript phrase to prepare for the next WQL command:  Select * from Win32_ComputerSystem".  The part of Win32 that we are particularly interested in is _ComputerSystem.  Win32 has dozens of possible properties, here we need to query neither the disk nor processor, but the ComputerSystem component.

From a VBScript perspective

4) Even though there is only one computer, the script still needs the loop: For Each....In... Next.  Believe me, I tried to simplify the script by removing the loop, but all I got was an object required error.

5) In this example .TotalPhysicalMemory is the property, which interests us most, however, in other examples we would substitute different properties, for example .NumberOfProcessors or .SystemType.

6) The line objComputer.Name is not strictly necessary, but it amuses me to display the correct machine name even thought the script uses strComputer = "."

7) VBScript does not understand word-wrap, so if the command spans two lines we add the _ (Underscore) at the end of the first line.

Guy Recommends SolarWinds' Free Network MonitorSolarwinds Network Device Monitor

Thus utility makes it easy to check the health of a router or firewall.  Check the real-time performance, and availability statistics, for any device on your network.  Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl - it's free. Download your free Network Device Monitor

If you need more comprehensive network analysis software:
Download a free trial of NPM (Network Performance Monitor)

Example 2 - WMI Memory Script with an Input Box

This script builds on Example 1 and adds code for an inputbox.  The result is greater flexibility to choose any machine on the network.  As a bonus, it includes a simple maths routine to display the TotalPhysicalMemory (RAM) in mb not bytes.

' Memory.vbs
' Sample VBScript to discover how much RAM in computer
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.4 - December 2010
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strComputer, intRamMB

strComputer = "."
strComputer = InputBox("Enter Computer name", _
"Find Computer Memory", strComputer)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
   intRamMB = int((objComputer.TotalPhysicalMemory) /1048576)+1
   Wscript.Echo "System Name: " & objComputer.Name _
   & vbCr & "Total RAM: " & intRamMB & " Mb"
Next

WScript.Quit

' End of free example of Memory WMI / VBScript

WMI Tutorial - Learning Points

1) The extra learning points in example 2 concern VBScript rather than WMI.  Most sample scripts use strComputer = ".", which refers to the current machine where you run the script.  By introducing an inputbox, you can easily change this value to the NetBIOS name of any machine on your network.  Surprisingly, an IP address did not work, it had to be a machine name.

2) I love variables, so this is why I introduced intRamMB to control a simple math calculation, which coverts bytes to megabytes.  The purpose is to display a more meaningful unit of RAM memory.

Note:  TotalPhysicalMemory may not be precise; there could be a slight understimate if the BIOS is using some of the physical memory.  Microsoft recomend employing Win32_PhysicalMemory and using the Capacity property.  See more about the Win32_ComputerSystem class.  Or else choose the PowerShell method below.

Simple PowerShell Example:  Display CIM_PhysicalMemory

A simple script to display your computer's physical memory.

# A simple command to display CIM_PhysicalMemory
Get-wmiobject CIM_PhysicalMemory

Those with a VBScript, or SQL background may be more familiar with query and select statement.  This achieves the same result, but is only useful if you wished to modify "Select *".

# Display CIM_PhysicalMemory using -query and "Select"
Get-wmiobject -query "Select * from CIM_PhysicalMemory"

See more about Powershell and WMI memory.

Summary - WMI Memory

These WMI examples show how to measure physical data such as TotalPhysicalMemory (RAM).  As you acquire VBScript skill, so you can tune up your scripts with an inputbox.  Once you have mastered one Win32 object then you can apply the method to investigate objects such as Win32_PhysicalDisk or Win32_Processor.

If you like this page then please share it with your friends

 


See more VBScript WMI examples:

WMI Tutorial   • Win32_Process   • WMI Memory   • WMI Basics   • Free Download of WMI Monitor

WMI VBS   • VBScript Services   • WMI Disks   • WMI Physical Disks

WMI Home   • WMI Win32   • WMI Printer   • VBScript Echo   • WMI VBScript

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: