• No results found

WMI Scripting Memory

In document Introduction to VBScript (Page 51-55)

WMI (Windows Management Instrumentation)

Introduction to WMI

In earlier pages we met Logon Scripts, File System Objects and Active Directory scripts. The WMI branch of the VBScript family has a different look and feel from other members. Now, to appreciate the beauty of a jewel such as WMI, you should view the crystal from different angles. To gain perspective and

understanding we will examine WMI from these five different angles:

1. See WMI as a method to automate the collection of hardware and software data.

2. Regard WMI as a database holding information about computer disk, services and processor and objects.

3. View WMI as a pipe connecting magically to the inner secrets of the Microsoft operating system.

4. Approach WMI as a distinctive dialect of VBScript with its own WQL vocabulary.

5. Treat WMI as a tool rather like a microscope to probe, and measure, the operating system's properties.

When you think about it, the operating system knows everything!, Windows

Server 2003 must know how much memory each process is using, how much free space there is on each partition, which devices are on which Bus. With WMI

scripting, you can query CIM library and view information about any aspect of the operating sytem.

WMI Topics

Example 1 - Discover your Computer's Memory WMI Tutorial - Learning Points

Example 2 - WMI Memory Script with an Input Box Summary - WMI Memory

Example 1

-

Discover your Computer's Memory

Here is a VBScript to discover how much Memory (TotalPhysicalMemory) there is in a particular machine. What we need to do is interrogate the CIM (Common Information Model) class called Win32_ComputerSystem. Calling for CIM is rather like going to the library and asking the assistant to get books from the archive. The first example examines the GetObject and ExecQuery, while the second example script adds flexibility with an InputBox.

Prerequisites for your WMI Script

No specific requirements. This script should display the amount of memory on any computer.

Introduction to VBScript.doc By Guy Thomas

Page 51 of 59

Instructions for Creating your Memory WMI Script

1. Copy and paste the example script below into notepad or a VBScript editor.

2. Save the file with a .vbs extension, for example: Memory.vbs

3. Double click Memory.vbs and check the TotalPhysicalMemory (Memory).

Script to Discover how much Memory (RAM) is in your computer

' Memory.vbs

' Sample VBScript to discover how much memory in your computer ' Author Guy Thomas http://computerperformance.co.uk/

' Version 1.2 - September 2005

' ---' Option Explicit

Dim objWMIService, objComputer, colComputer Dim strLogonUser, strComputer

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\ " _ & strComputer & "\ root\ cimv2")

Set colComputer = objWMIServi ce.ExecQuery _ ("Select * from Win32_ComputerSystem") For Each objComputer in colComputer

Wscript.Echo "Computer Name: " & objComputer.Name _ & vbCr & "Total Memory " & objComputer.TotalPhysicalMemory Next

WScript.Quit

' End of Memory WMI / VBScript

WMI Tutorial

-

Learning Points

From a WMI perspective

Note 1: We need to connect to the Root of the CIM namespace. The first procedure is to tell winmgmts to access the root of the CIM library. Study this line carefully as it crops up in so many WMI scripts :

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

Each of the above words is instructive, once again we create an object called objWMIService. Observe how we tell GetObject that we want a "winmgmts:" type of object, and not a shell or network object.

Note 2: In most WMI examples, we need security clearance to query the other machine's hardware, this is why we add :

& "{impersonationLevel=impersonate}!\\ " _

To begin with, just trust me and accept that WMI scripts usually need this command to be sure that you have sufficient rights to query the operating system.

Introduction to VBScript.doc By Guy Thomas

Page 52 of 59

Note 3: Set colComputer = objWMIService.ExecQuery _ Here we have a standard VBScript phrase to prepare for the 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 we do want is the ComputerSystem component.

From a VBScript perspective

Note 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' 800xxxxx error.

Note 5: In this example, .TotalPhysicalMemory is the property that displays the amount of RAM. However, in other examples we would substitute different properties depending on our purpose, for example .NumberOfProcessors or .SystemType.

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

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 for you to point the the script to any machine on the network. As a bonus, it includes a simple arithmetic routine to display the

TotalPhysicalMemory (RAM) in mb not bytes.

' MemoryInput.vbs

' VBScript with Input box, to discover how much RAM in computer ' Author Guy Thomas http://computerperformance.co.uk/

' Version 1.4 - September 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 Memory: " & intRamMB & " mb" Next

WScript.Quit

Introduction to VBScript.doc By Guy Thomas

Page 53 of 59

WMI Tutorial

-

Learning Points

Note 1: The extra learning points in Example 2 concern VBScript rather than WMI. Most example scripts use strComputer = ".", which refers to the current machine. (The computer where you run the script.) By adding an Inputbox() command, you can easily change the computer value to the NetBIOS name of any machine on your network, at run time. Surprisingly, an IP address did not work, it must be a machine name.

2)The other extra feature of this code is to perform a simple math calculation and covert bytes to megabytes, which is a more meaningful unit of RAM memory.

Summary

-

WMI Memory

These WMI examples show you 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, in this case Win32_ComputerSystem.TotalPhysicalMemory, then you can apply the same method to investigate objects such as disk or processor. Just amend the above method by substituting Win32_PhysicalDisk or Win32_Processor.

This page covered the following VBScript methods, commands and syntax:

Introduction to VBScript.doc By Guy Thomas

Page 54 of 59

In document Introduction to VBScript (Page 51-55)

Related documents