Introduction to How Much RAM Memory is in the Computer?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
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.
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 ScriptNo 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
- Copy and paste the example script below into notepad or a VBScript editor.
- Decide which machine to interrogate and then change line 10 accordingly:
my default for strComputer = "." means the current machine.
- Save the file with a .vbs extension, for example: Memory.vbs
- 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 2005 '
-------------------------------------------------------' 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
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.
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
2005 ' -------------------------------------------------------' 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 Points1) 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. 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.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
See Also
● WMI Home ● Simple WMI Script ● Terminate a
Windows Process ● Start / Stop Services |