Contents of Guy’s Scripting Ezine 9 – WMI Part 1
Note, See more on WMI a whole section here
- Why and where you can use WMI scripts
- This Week’s Secret
- Script revisited
- This week’s scripts
- Learning points
- Troubleshooting and resources
Why and where you can use WMI scripts
Why bother to learn WMI (Windows Management Instrumentation) scripting? Well here are four reasons.
- You want change settings automatically – say at logon.
- You want to gather operating system information on a remote machine – There is no easy interface.
- Save repetitive tasks – change a department name.
- To me the best reason is because WMI scripting is the way of the future.
This Week’s Secret
To master WMI, you really need to read three different accounts, or have three different people explain the concepts behind WMI. For now you will have to make do with Guy wearing three different hats.
Mr Deep Theory
The operating system knows everything! When you think about it, Windows Server 2003 or XP must know how much memory each process is using. When it booted, the operating system checked on which services need to start and in which order. It made sense for Microsoft to produce a monitor or manager which could share such information on system resources, well that monitor is WMI.
The final link in our chain is VBScript. We right lines of code which gather system information, or we write scripts which can control the registry or Active Directory. Well thank you Mr Deep Theory, that is enough for this week.
Mr Infrastructure Manager
Let us take stock of WMI. Here are there are 4 components:
CIMOM | CIM Repository | Providers | WMI Script Library "winmgmts" |
Common Information Object Manager
| Schema for dynamic resources | Active Directory Registry Event Log Win32 | Consistent model to read or modify objects. Supports VBScript, Jscript and ActivePerl |
Keyword – Manager | Keyword – Schema | Key concept – service | Key concept – interpreter |
Mr Vis Basic
What is interesting about the Providers, is their namespaces. From other of my scripts you may have already seen ‘root\cimv2’. This is a portal to the Event Log and all the Win 32 processes. Active Directory can be found through root\directory\ldap.
Perhaps you remember this script from Ezine 6? Let us dissect some of the relevant terms. (If you do run this script, be patient, it takes ages to check through a big log.)
For much more on WMI, see my WMI Section here.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Script – revisited:
‘VBScript to check the System Event log for Improper Shutdowns.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = ‘System’ and " _
& "EventCode = ‘6008’")
Wscript.Echo "Improper shutdowns: " & colLoggedEvents.Count
- strComputer = "." This means use the current machine. You could substitute strComputer = "BigServer" (Where BigServer = a remote server)
- "winmgmts" is an alias for the script library. Set objWMIService = GetObject("winmgmts:" _ Here the script contacts the WMI manager and connects to the that library.
- " & strComputer & "\root\cimv2". This phrase contacts the Event Log Provider cimv2 (the default is root\default). If you needed to access Active Directory, then the reference would \root\default\ldap, instead of \root\cimv2.
This week’s script
The idea behind this week’s script is to persuade WMI to interrogate the operating system. We are going to discover facts that are not easily found by browsing through GUIs. There is no one interface that displays all the information created by the script. However the actual data is not important, what is exciting is that thanks to scripting, we can interrogate Windows Server 2003 in any way that we wish.
If the output gives you just one idea for a script of you own then I will be happy. My point is, that there is no need to collect ALL this data, however, one or two items in the output may spark an idea for a new project.
Instructions.
This week I urge you to type this script rather than copy paste. Now I don’t often make this request, but to get the feel of what is going on I exhort you to print it out and type it in, just to get an understanding of what the script is achieving. Dare I say, if you get the odd syntax error, then you will learn even more.
Script 1 (Short)
‘ VBScript to interrogate the operating system
strComputer = "."
set objWMIService = GetObject("Winmgmts:\\" & strComputer)
set colOperatingSystems = objWMIService.InstancesOf("win32_OperatingSystem")
For each objOperatingSystem In colOperatingSystems
WScript.echo "Name : " & objOperatingSystem.name & vbCRLf & _
"Free Virt Mem : " & objOperatingSystem.freevirtualmemory & vbcrlf & _
"Current Users : " & objOperatingSystem.numberofusers & vbcrlf
Next
Learning Points from the WMI Script
- strComputer = "." I expect you remember that this means – local machine. I challenge you to change the "." to the name of different computer on your network.
- set objWMIService This is different from logon scripts which begin – Set WshShell Here we connect to WMI rather than prepare a network shell connection.
- InstancesOf This is useful Method to case the win32_OperatingSystem object.
- For each ……. next. Creates a loop to keep checking the OperatingSystem object.
- Col refers to a ‘Collection’, similar to an array. Note error 800A01C3, means that you misspelled OperatingSystems or for get the final ‘s’.
- WScript.echo is an old friend to produce a message box.
- & VBCRLF This is useful control feature to produce a carriage return.
- Beware if you copy and paste to reorder the lines, the last line has a shorter ending that the others. & vbcrlf is correct (not & vbcrlf & _)
Script 2 (Full)
Do copy and paste this fuller version. Unless you are feeling virtuous, in which case just type in the extra lines!
‘ VBScript to interrogate the operating system
strComputer = "."
set objWMIService = GetObject("Winmgmts:\\" & strComputer)
set colOperatingSystems = objWMIService.InstancesOf("win32_OperatingSystem")
For each objOperatingSystem In colOperatingSystems
WScript.echo "Name : " & objOperatingSystem.name & vbCRLf & _
"CS Name : " & objOperatingSystem.csname & vbcrlf & _
"Caption : " & objOperatingSystem.caption & vbcrlf & _
"Version : " & objOperatingSystem.version & vbcrlf & _
"OS Type : " & objOperatingSystem.OsType & vbcrlf & _
"Service Pack : " & objOperatingSystem.ServicePackMajorVersion & vbcrlf & _
"Locale : " & objOperatingSystem.Locale & vbcrlf & _
"Time Zone : " & objOperatingSystem.CurrentTimeZone & vbcrlf & _
"Install Date : " & objOperatingSystem.installdate & vbcrlf & _
"Boot Device : " & objOperatingSystem.BootDevice & vbcrlf & _
"Free Phys Mem : " & objOperatingSystem.freephysicalmemory & vbcrlf & _
"Free Virt Mem : " & objOperatingSystem.freevirtualmemory & vbcrlf & _
"Current Users : " & objOperatingSystem.numberofusers & vbcrlf
Next
Troubleshooting Resources
- Problems with Code 800 errors see – See 800… section.
Ezine 10 – WMI Part 2
This week I just wanted to give you a taster of what WMI can do. In the next Ezine I will show you how to choose objects like OperatingSystem, and where you can get a list of properties like InstallDate or FreePhysicalMemory.
For much more on WMI, see my WMI Section here.
See More WMI Scripts
• WMI • Ezines • WMI Basics • WMI PowerShell • Free SolarWinds WMI Monitor
• Ezine 9 WMI • Ezine 10 WMI • Ezine 19 WMI • Ezine 40 Variables • Ezine 48 WMI
• Ezine 52 WMI OS • Ezine 76 WMI Classes • Ezine 93 WMI • Ezine 94 WMI • Ezine 95 WMI