Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


May 2001

Windows Management Instrumentation Scripting for Beginners


RSS
Subscribe to Windows IT Pro | See More Windows Management Instrumentation (WMI) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
SideBar    Adding WMI to NT 4.0 and Win95 Systems

Download the Code Here

Now let's look on this computer for an object to manage: the C drive. I don't know the exact name of this element in CIM or WMI, so rather than browse the hundreds of classes that CIM Studio lists, I click the Find button (the binoculars at the top of the lefthand pane). In the resulting Search for Class dialog box, which Figure 2 shows, I type the name of a keyword that I think will likely be in the C-drive class name and click Go! Because I'm looking for a named partition and I know that Windows OSs call such partitions logical disks or logical drives, I search for the keyword logical. You could also search for disk, but you'd have to scroll through a lot more results.

Figure 2 shows the available classes. I select Win32_LogicalDisk and click OK, and the window in Figure 3 appears. (Why not CIM_LogicalDisk? As I mentioned previously, Win32 is the prefix for any object WMI manages. If you select CIM_LogicalDisk and try to display instances, you won't see any specific information about the available logical disks—you'll just see a Win32_LogicalDisk entry for each available logical drive.) The righthand pane now shows the available properties for the Win32_LogicalDisk class. Note that the property values are empty because we're looking at the class, not at a particular instance of the class. To display the instances of the Win32_LogicalDisk class, I click the Instances button at the top of the righthand pane (fourth button from the right).

The resulting window lists all the instances of logical disks on this particular machine, including network-accessible logical drives, in the righthand pane. I click the instance with device ID "C:". The resulting window, which Figure 4 shows, displays the properties and methods of the selected instance on tabs in the righthand pane. The name of the logical disk you selected appears at the top of the righthand pane.

You can use a script to change the properties or to apply the methods. If you have questions about the properties, select the Win32_LogicalDisk class or Win32_LogicalDisk.DeviceID="C:" instance and click the Help button. You'll see documentation for most of the object's available properties and methods.

Starting to Script
Now that you've had a look at the Win32_LogicalDisk class and its properties, I want to give you a feel for working with this information in a script. If the Microsoft Windows 2000 Resource Kit is installed on your computer, listfreespace.vbs is located by default in the \program files\resource kit folder. This script queries each object in the Win32_LogicalDisk class for the values of a couple of its properties: DeviceID (its drive letter) and FreeSpace (the number of bytes of free space it has). On a computer where A is the floppy disk drive and D is the CD-ROM drive, listfreespace.vbs's output might look something like this:

A:  not available
C:  8,243,381,248  bytes
D:  not available
G:  4,776,943,616  bytes

You can use getfree.vbs, which Listing 1 shows, to obtain similar information with less code. I've heavily commented the script so that you can tell exactly how it works. You supply a drive letter when you invoke the script, and getfree.vbs tells you the free space and file system for the drive. For example, if I type

getfree.vbs c:

on my laptop, I see the window that Figure 5 shows.

Now you know what WMI is and how to find managed objects on your computer. You might not yet be an expert WMI "scripter," but you'll know what pieces to manipulate and where they are when you write scripts.

Related Articles in Previous Issues
You can obtain the following articles from Windows 2000 Magazine's Web site at http://www.win2000mag.com.

DARREN MAR-ELIA
"Leveraging Windows Management Instrumentation in Win2K Pro," Summer 2000, InstantDoc ID 8819
MARK RUSSINOVICH
Internals, "Inside Windows Management Interface," February 2000, InstantDoc ID 7937
BOB WELLS
Scripting Solutions, "WMI Monikers," May 2001, InstantDoc ID 20401
Scripting Solutions, "Windows Management Instrumentation: The Systems Administrator's Apprentice," August 2000, InstantDoc ID 9033
Scripting Solutions, "Windows Management Instrumentation: The Journey Begins," July 2000, InstantDoc ID 8959

End of Article

   Previous  1  [2]  Next  


Reader Comments
Hi
i badly need of a script
I will just input the from&to ip address and an exe name
The script should report me the ip address of the machines which contains that exe
can any one pls help me reg this.

sathish June 10, 2002


The download for the WMI SDK is incorrect. Here is the correct URL (after much wading through M$'s site):
http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/msdn-files/027/001/566/msdncompositedoc.xml

Lane June 27, 2002


Nice article. Very interested in subject and finding out more. URL doesnt work ???http://www.msdn.microsoft.com/downloads/sdks/wmi/download.asp

Tim Miller June 27, 2002


Looks like MS did a site move on you. Here is a good URL for today, 8/13/02, http://www.microsoft.com/msdownload/platformsdk/sdkupdate/.
I was surprised to find that while the WMI SDK was only 7.7 MB, it required the Core SDK which was over 200 MB. Be prepared for 5 - 10 minutes of download time and allow for the space. Otherwise a good introduction to WMI Scripting.

Preston Gillmore August 13, 2002


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Windows 7 Sets Sales Record

Microsoft CEO Steve Ballmer described Windows 7's first ten days of sales as "fantastic" while in Japan yesterday. ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events WinConnections and Microsoft® Exchange Connections

Deep Dive into Windows Server 2008 R2 presented by John Savill

Cutting Costs with Client Management

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement