Contents for Guy’s Scripting Ezine 48 – WMI Explained
- This Week’s Secret
- WMI in context
- Example to display information about network cards.
- Learning Points
I now believe that poetry is the highest form of writing. It was not always so. As a teenager, my gang thought that poetry was only for wimps. (Only we used an even less politically correct word for wimps.) I’ll let you into a secret, I would now love to be able to write effective, memorable poetry.
‘What has poetry got do with scripting?’, I can hear you asking, The answer is that both scripting and poetry place a premium on choosing each word carefully. The result is that in a VBScript, as with a poem, every phrase expresses just the right shade of meaning. Let us take ‘GetObject’ as an example from this week’s script. FetchObject would not be understood by the VBScript engine, while CreateObject is not what I wish to achieve with my NetworkConnection. We must employ the correct word, GetObject, so that VBScript knows how to processes our instructions precisely.
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.
This Week’s Mission
My mission this week is to explain the WMI (Windows Management Interface) syntax. Here below are two ‘Set…’ statements which will act as examples for us to explore the meaning of each power-packed WMI word.
Set objWMIService = GetObject("winmgmts:\\" & strComputer & _
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_NetworkConnection",,48)
If the above statements seem incomprehensible at first, do not worry, the mist will clear and you will discover a valuable scripting technique.
In a nutshell, WMI gives us a method to see what the operating system is doing. Moreover, WMI provides ‘handles’ to persuade Windows 2003 or XP to return information in a form that we can use to our advantage.
This week’s WMI example will uncover information about the network card (NIC). If fact, I amended this script from a project to inform users when their wireless connection was down. Yes, even wireless connections have a network card.
The WMI script works at 4 levels,
- Just a script to interrogate the network cards(s).
- An opportunity to examine each word of a WMI statement. See comprehensive Learning Points below the script.
- Using Win32_NetworkAdapter as but one examples of many WMI objects. To see other objects, for example Win32_NetworkConnection, then check out scriptomatic
- For the final level, instead of creating the VBScript with WMI instructions yourself, you buy off the shelf tools to analyze your computer network for you. See here: Tools4Ever
- Copy and paste the script below into notepad.
- Save the file with .vbs extension e.g. NIC.vbs.
- Double click and examine the message box.
‘ Set NIC.vbs
‘ VBScript to check your network connection.
‘ Author Guy Thomashttp:// computerperformance.co.uk/
‘ Version 4.9 – October 3th 2004
Dim objWMIService, ObjItem
Dim GuyMessage, strComputer, colItems
‘On Error Resume Next
strComputer = "."
‘ These Set commands are important.
‘ See Learning Points after / below the script
Set objWMIService = GetObject("winmgmts:\\" & strComputer _
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapter",,48)
For Each objItem in colItems
If objItem.MACAddress <> "" Then
‘ Remove objItem’s that you do not want.
WScript.Echo "Name: " & objItem.Name & vbCRLf & _
"Net Connection ID: " & objItem.NetConnectionID & vbCRLf & _
"Net Connection Status: " & objItem.NetConnectionStatus & vbCRLf & _
"Adapter Type: " & objItem.AdapterType & vbCRLf & _
"MAC Address: " & objItem.MACAddress & vbCRLf & _
"Availability: " & objItem.Availability & vbCRLf & _
"" & vbCRLf & _
"Computer Name: " & objItem.SystemName & vbCRLf & _
"AutoSense: " & objItem.AutoSense & vbCRLf & _
"ManagerErrorCode: " & objItem.ConfigManagerErrorCode & vbCRLf & _
"ManagerUserConfig: " & objItem.ConfigManagerUserConfig & vbCRLf & _
"DeviceID: " & objItem.DeviceID & vbCRLf & _
"ErrorCleared: " & objItem.ErrorCleared & vbCRLf & _
"ErrorDescription: " & objItem.ErrorDescription & vbCRLf & _
"Index: " & objItem.Index & vbCRLf & _
"InterfaceIndex: " & objItem.InterfaceIndex & vbCRLf & _
"LastErrorCode: " & objItem.LastErrorCode & vbCRLf & _
"Manufacturer: " & objItem.Manufacturer & vbCRLf & _
"MaxNumberControlled: " & objItem.MaxNumberControlled & vbCRLf & _
"MaxSpeed: " & objItem.MaxSpeed & vbCRLf & _
"NetworkAddresses: " & objItem.NetworkAddresses & vbCRLf & _
"PermanentAddress: " & objItem.PermanentAddress & vbCRLf & _
"PNPDeviceID: " & objItem.PNPDeviceID & vbCRLf & _
"Power Mngmnt: " & objItem.PowerManagementCapabilities & vbCRLf & _
"Power Supported: " & objItem.PowerManagementSupported & vbCRLf & _
"ServiceName: " & objItem.ServiceName & vbCRLf & _
"Speed: " & objItem.Speed & vbCRLf & _
"Status: " & objItem.Status & vbCRLf & _
"StatusInfo: " & objItem.StatusInfo & vbCRLf & _
"CreationClassName: " & objItem.CreationClassName & vbCRLf & _
"TimeOfLastReset: " & objItem.TimeOfLastReset & vbCRLf & _
‘ End of example VBScript
Note 1: SetObjWMIService is a name that I chose for the variable, almost all the rest of the terms are defined by WMI or VBScript.
Note 2: GetObject( This is the command to extract the information from the Network Card. On other occasions it could be CreateObject(.
Note 3: "Winmgmts:\\" This tells the script to connect to the Windows Management Interface, not to the File System or to Excel.
Note 4: & strComputer _& "\root\cimv2") This defines the precise area of WMI to connect. In fact, it means connect to the root of the computer specified by strComputer. Looking further back we see this is "." meaning "This computer".
So put it all together and the statement says, "Connect to the WMI root of the computer where the script is run".
Note 5: Set colItems. In this context colItems means a collation or collection of items. (I used to think it meant columns – wrong!)
Note 6: objWMIService.ExecQuery. Perhaps you had forgotten about that first variable? Well this is where we query objWMIService.
Note 7: Select * from. In this context, * (star) means all. This phrase is well know from SQL or other database query languages. In certain circumstances you could amend the * and so filter data and return the subset that we are interested in.
Note 8: Study Win32_NetworkAdapter carefully. With help from VBsEdit you could substitute a whole host of other operating system objects, all beginning with WIN32_, for example: WIN32_processor, eventlog, registry.
Note 9: ,,48 Confession, I have no idea what this means, I just leave it be. However! JT, kindly provided this information to enlighten me (us).
Note 10: objItem.xxxxx this is the interesting part where you can select which properties of the NetworkAdapter that you wish to display.
For much more on WMI,see my WMI Section here.
WMI provides fabulous hooks and tentacles to extract information about all aspects of the operating system. In this week’s example we dissect the command which interrogate the network adapter, however you can adapt this method to enquire about virtually any aspect of the operating system. For example, memory, disk or eventlog. To help you research other WMI objects then check out scriptomatic
See More WMI Scripts