Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


February 26, 2001

Progressive Perl for Windows: Using WMI to Request Data About Disk Drives


RSS
View this exclusive article with VIP access -- click here to join |
See More Perl Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

Last month, I discussed EventMon.pl, a script that monitors events in logs on remote Win32 machines and displays those events in realtime. The magic behind this script is Windows Management Instrumentation (WMI). In my explanation of EventMon.pl, I mentioned that you ought to become familiar with WMI. This month, I insist on it.

Many of you are probably rolling your eyes right now and moaning about yet another Microsoft technology to learn. WMI is well worth learning because it offers a rich set of features that will meet most of your administrative needs. With WMI, your Perl scripts can query a machine's settings, processes, hardware, and more. These scripts can even monitor events that a remote machine fires.

To help you learn WMI, I explain how to access WMI from Perl. In addition, I provide a practical example of how you can use WMI with Perl. I don't focus on the nitty-gritty but rather give a cursory overview of the information you need to start using this technology. . . .

Reader Comments
<p>I am using WMI with Perl, but whenever I access the logical disks using Logical_disk class, the script scans the floppy drives as well. I am trying to find a way so that I can get data only about logical disk without scanning floppy drives every time script runs (as this may damage floppy drives on the server, if I run the script very frequently).</p>

Sanjeev Shukla June 17, 2002


<p>You can use the ExecQuery() method off of the WMI Services script object to solve this. The query is very similar to SQL and would allow you to specify that you want all Win32_LogicalDisk instances where the DeviceID is greater than or equal to "C:". You could make more sophisticated queries such as collect all instances between drives D: and L:, if you like. Try this Perl code:<br>
<br>
<pre>
use strict;<br>
use Win32::OLE qw( in );<br>
$Machine = ".";<br>
my $WMIServices = Win32::OLE->GetObject<br>
( "winmgmts:{impersonationLevel=impersonate,<br>
(security)}//$Machine/root/cimv2" ) || die;<br>
my $DriveCollection = $WMIServices->ExecQuery<br>
( 'select * from Win32_LogicalDisk where<br>
DeviceID >= "C:"' ) || die "Query Failed";<br>
foreach my $Drive ( in( $DriveCollection ) )<br>
{<br>
my $FreePercent = int( ( $Drive->{FreeSpace} / $Drive->{Size} ) * 100 );<br>
print "$Drive->{DeviceID}\n";<br>
print "\tVolume: $Drive->{VolumeName}\n";<br>
print "\tDisk Size: " . FormatNumber( $Drive->{Size} ) . "\n";<br>
print "\tAvailable: " . FormatNumber( $Drive->{FreeSpace} ) . "<br>
($FreePercent%)\n";<br>
print "\n";<br>
}<br>
<br>
sub FormatNumber<br>
{<br>
my($Number) = @_;<br>
my $Suffix = "";<br>
if( $Number > 1024 * 1024 * 1024 )<br>
{<br>
$Number /= (1024 * 1024 * 1024);<br>
$Suffix = "G";<br>
}<br>
elsif( $Number > 1024 * 1024 )<br>
{<br>
$Number /= (1024 * 1024);<br>
$Suffix = "M";<br>
}<br>
elsif( $Number > 1024 )<br>
{<br>
$Number /= 1024;<br>
$Suffix = "K";<br>
}<br>
$Number = sprintf( "%0.2f", $Number );<br>
while( $Number =~ s/^(-?\d+)(\d{3})/$1,$2/ ){};<br>
return( $Number . $Suffix );<br>
}<br>
</pre>
</p>


Dave Roth August 23, 2002


We can find the drives on a machine using Win32_LogicalDisk and can find target lun bus numbers using Win32_DiskDrive . but how to find the mapping between the drive letters and targets/lun/bus .

Rahul Bhiide June 24, 2004


no comment

Anonymous User February 20, 2005


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Command Prompt Tricks

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

WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...


Related Events 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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing