Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  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!

Step 4
The last step is to use the Win32::OLE->in() function to enumerate the class instances. This function returns an array that contains each class instance found in the COM collection object. You might recall that you imported the in() function in Step 1. Because you imported the function at that time, you can simply refer to the function as in() in this step.

You pass the in() function the collection of instances ($DriveCollection), as Listing 1 shows. With each iteration of the foreach loop, the $Drive variable contains a COM-based instance of the Win32_DiskDrive class. In this case, you're just enumerating the drives, but you can query properties and call methods on $Drive as you would any other COM object.

These four steps provide a script with disk-drive information from a remote machine. Let's now look at a real-world example of how you can use the Win32_DiskDrive class in a script.

A Practical Example
Listing 2 contains a real-world script called WDiskDrives.pl. The Win32_DiskDrive class doesn't contain any writable properties or methods. Thus, WDiskDrives.pl explores the target machine's disk drives by querying 7 of the class's 48 properties:

  • Capabilities (returns a list of the drive's capabilities)

  • InterfaceType (returns the type of interface the drive uses, such as SCSI or IDE)

  • MediaType (returns the type of media that the drive uses)

  • Model (returns the drive's model number)

  • Name (returns the OS's physical path to the drive)

  • Partitions (returns the number of partitions on the drive)

  • Size (returns the drive's size in bytes)

Let's begin exploring WDiskDrives.pl by finding the four steps just discussed. In Listing 2, callouts A, B, C, and D highlight the code for Steps 1, 2, 3, and 4, respectively. . . .


Already a VIP member?
Please log on to view the full article

Why become a VIP member?

VIP-only online access
VIP CD delivered twice a year: offline access to the entire Windows IT Pro article library
Monthly issue of your choice of Windows IT Pro or SQL Server Magazine

Subscribe Now
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 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. ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


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

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 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