Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Remote Drive Info

by raj8 (Sexton)
on Apr 30, 2004 at 16:45 UTC ( #349448=perlquestion: print w/replies, xml ) Need Help??
raj8 has asked for the wisdom of the Perl Monks concerning the following question:

I cannot for the life of me find out why my script is not listing drive letters. I feel like the reg expression is not working correctly, but it does not error out. Any ideas
use Win32; use Win32::OLE qw( in ); %TYPE = ( 0 => 'Unknown', 1 => 'No Root', 2 => 'Removable', 3 => 'Local', 4 => 'Network', 5 => 'CDRom', 6 => 'RAM Disk', ); push (@ARGV, Win32::NodeName() ) if( ! scalar @ARGV ); foreach my $Machine ( @ARGV ) { $Machine =~ s/\\+//; my $CLASS = "WinMgmts:{impersonationLevel=impersonate}!//$Machine"; print "\\\\$Machine\n"; if( my $WMI = Win32::OLE->GetObject( $CLASS ) ) { local $Disk; $~ = DISK_HEADER_FORMAT; write; $~ = DISK_FORMAT; foreach $DISK ( in( $WMI->InstancesOf( "Win32_LogicalDisk" ) ) ) { local $Drive = $TYPE{$Disk->{DriveType}}; local %Size = ( total => FormatNumber( $Disk->{Size} ), free => FormatNumber( $Disk->{FreeSpace} ) ); write; } } print "\n"; } sub FormatNumber { my( $Number ) =@_; while( $Number =~ s/^(-?\d+)(\d{3})/$1,$2/ ){}; return( $NUMBER ); } format DISK_HEADER_FORMAT = FS Type Size Free --------------------------------------------------------- . format DISK_FORMAT = @<< @<<<< @<<<<<<<< @>>>>>>>>>>>>>>>>>>>> $Disk->{Name}, $Disk->{FileSystem}, $Drive, $Size{total}, $Size{fre +e} .

Replies are listed 'Best First'.
Re: Remote Drive Info
by Paladin (Priest) on Apr 30, 2004 at 17:16 UTC
    There are a few problems here.
    1. Your foreach loop uses $DISK to store each item, then you go and access $Disk. Perl is case-sensitive, either change the first to $Disk or change the rest to $DISK
    2. $Disk->{Size} and $Disk->{FreeSpace} seem to be returning undef, so those 2 columns of your output are blank as well.
    3. You have 4 format fields in your DISK_FORMAT and 5 variables below it. Not sure if you want another field, or if you want to get rid of a variable there.
    4. Again, in your FormatNumber sub, you work on $Number, but return $NUMBER.
    Other than those 3, the code seems to work fine, although there are other small problems such as formatting, and the use of local that could use fixing up.

    Update: Spotted the error in FormatNumber

      ++Paladin. I would just add:
      • By adding either #!perl -w or use warnings 'all'; to the top of your program, you would receive diagnostic messages that would have helped you find the problem; i.e.
        Name "main::NUMBER" used only once: possible typo at line 47
        Name "main::DISK"   used only once: possible typo at line 31
        Don't forget also use strict!

Re: Remote Drive Info
by shonorio (Hermit) on Apr 30, 2004 at 23:28 UTC
    Just one thing, you can get one example of WMI getting information from hardware by WMI at Hardware/Software Inventory

    Solli Moreira Honorio
    Sao Paulo - Brazil
Re: Remote Drive Info
by crabbdean (Pilgrim) on May 01, 2004 at 04:19 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://349448]
Approved by Courage
Front-paged by Courage
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2017-02-27 16:14 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (388 votes). Check out past polls.