3 Simple steps to WMI script
In 1998, Microsoft released the Windows Management Instrumentation (WMI) as part of service pack 4 to NT 4.0. Now WMI is an integral component of Windows XP, Windows 2000 and Windows 2003. WMI was designed to provide a consistent way of accessing management information across the network. For system administrator, understanding how to use WMI is just as important as understanding Active Directory directory services.
Using Windows Script Host and Microsoft Visual Basic Script (VBscript), Microsoft Jscript, or any scripting language that interface and support COM automation, you can write WMI script that improve and automate the management of the following systems:
Windows 2000, XP and Server 2003:
You can write scripts to query and manage the event log, file systems, printers, processes, registry, tasks, security, services, shared folders, configuration and component settings.
Network:
You can write script to manage network services such as DNS, client networking settings, DHCP, and SNMP.
Monitor system health:
You can write script to monitor system health by using WMI event subscription.
Windows .NET Enterprise server applications:
You can write scripts to manage Microsoft Enterprise software such as Exchange Server, SQL Server, Operations Manager, and Systems Management Server.
This is a wide range of application for WMI if you know now to exploit them. In many cases with WMI scripts, you can replicate the same functionalities available in command line tools. The significant difference with command line tools is that you have to execute them at a computer, but with WMI script, you can execute them over your network one by one without leaving your desk!
I will not go into the detail of WMI components and structures. There is a wealth of information from Microsoft Scripting Guide Chapter 6: WMI Scripting Primer or
MSDN: WMI SDK section at:
http://msdn.microsoft.com/library/de...start_page.asp
In this article, we are simply dealing with applying the information to automate our management and not with the theory and design of WMI. To write a WMI script, it is a simple procedure. In general, you have to know where the information are stored in WMI and write a script to access the information.
WMI breaks the information into classes. You can use the scriptomatic tools from MS Scripting Guide or some VB editor will give the syntax and parameters. I used Adersoft’s VbsEdit to speed up my learning curve and to avoid the typing mistakes. It is available at http://www.vbsedit.com for $30. Generally speaking to write a WMI script involve three steps:
1. Each script started by connecting to a WMI service.
2. Retrieve a WMI object or a collection of objects.
3. Perform some sort of task on the object or objects.
Here is my first WMI script I wrote last year called w32opersys.vbs to display OS information:
Code:
strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer)
Set colOperatingSystems = objSWbemServices.InstancesOf("Win32_OperatingSystem")
For Each objOperatingSystem In colOperatingSystems
Wscript.Echo "CSName: " & objOperatingSystem.CSName & vbCrLf & _
"Name: " & objOperatingSystem.Name & vbCrLf & _
"Caption: " & objOperatingSystem.Caption & vbCrLf & _
"CurrentTimeZone: " & objOperatingSystem.CurrentTimeZone & vbCrLf & _
"BootDevice: " & objOperatingSystem.BootDevice & vbCrLf & _
"LastBootUpTime: " & objOperatingSystem.LastBootUpTime & vbCrLf & _
"LocalDateTime: " & objOperatingSystem.LocalDateTime & vbCrLf & _
"Locale: " & objOperatingSystem.Locale & vbCrLf & _
"Manufacturer: " & objOperatingSystem.Manufacturer & vbCrLf & _
"OSType: " & objOperatingSystem. OSType & vbCrLf & _
"Version: " & objOperatingSystem.Version & vbCrLf & _
"Service Pack: " & objOperatingSystem.ServicePackMajorVersion & _
"." & objOperatingSystem.ServicePackMinorVersion & vbCrLf & _
"Windows Directory: " & objOperatingSystem.WindowsDirectory & vbCrLf & _
"SystemDirectory: " & objOperatingSystem.SystemDirectory
Next
Let's examine the script. The first line is computer designation. strComputer = “.” ,indicates this is a local computer. You can change it to a remote computer, “SQLSERVER01”, the script will go to SQLSERVER01 and retrieve the necessary the information. Of course you can only this if you have administrator authority!
The second line is what I call connecting to the WMI service. In this case, we are using “winmgmts” at the local computer.
The third line is to create an instance or an object by using the class called “Win32_OperatingSystem”. Once this is done, we can then display the property of the class and vbCrLf is just giving it a better format appearance. For illustrative purpose, only a small subset of information is used here.
Code:
c:\scripts\tools\cscript w32opersys.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
CSName: SNOOPYNETSQL1
Name: Microsoft Windows Server 2003 Enterprise Edition|C:\WINDOWS|\Device\Harddisk0\Partition1
Caption: Microsoft(R) Windows(R) Server 2003, Enterprise Edition
CurrentTimeZone: -300
BootDevice: \Device\HarddiskVolume1
LastBootUpTime: 20040316105255.500000-300
LocalDateTime: 20040316144449.515000-300
Locale: 0409
Manufacturer: Microsoft Corporation
OSType: 18
Version: 5.2.3790
Service Pack: 0.0
Windows Directory: C:\WINDOWS
SystemDirectory: C:\WINDOWS\system32
If you do a little research and know the class and information available in each class, you are on your way to be a script writer. All you need is the program notepad! The key is to know what class and the information available in each class.
This is only a three lines script but the information behind this is powerful. If your user is telling you that he or she is having problem with her computer, you can use a script like this to display the computer configuration, services, maintenance level and other vital information. This is a powerful stuff. Once I got into scripting, I don’t know what to do without it!
Happy scripting!
|