Nemurenai has asked for the wisdom of the Perl Monks concerning the following question:

Most revered monks, according to http://www.microsoft.com/whdc/archive/smartdrv.mspx it should be possible to extract SMART drive info by using WMI. However, I can't figure out how exactly to go about it. This is what I've come up with so far:
use warnings; use Win32::OLE qw( in ); my $WMIServices; my $Machine = "."; $WMIServices = Win32::OLE->GetObject( "winmgmts:{impersonationLevel= impersonate,(security)}//".$Machine ); my $res = $WMIServices->ExecQuery("select * from MMStorageDriver_Failu +rePredictData"); foreach (keys %$res) { print "$_\n"; }
which outputs
Count Security_
It seems to me that WMI resources tend to have keys in the hash which don't show up in a 'foreach (keys %$foo)' - is this correct? And if so, is there any way to extract these keys? I've been unable to find out which ones WMI provides for SMART info. Thanks

Replies are listed 'Best First'.
Re: SMART info from drives in win32 using WMI
by wfsp (Abbot) on May 17, 2006 at 13:36 UTC
    Not an answer to your question but one way of looking at what is returned is to use Data::Dumper

    For me, print Dumper $res; returns:

    $VAR1 = bless( { 'Count' => undef, 'Security_' => bless( { 'ImpersonationLevel' => 3, 'AuthenticationLevel' => 6, 'Privileges' => bless( { 'Count' => 1 }, 'Win32::OLE' ) }, 'Win32::OLE' ) }, 'Win32::OLE' );
Re: SMART info from drives in win32 using WMI
by McDarren (Abbot) on May 17, 2006 at 13:38 UTC
    I don't know much about WMI resources, but I do know that when dealing with data structures - Data::Dumper::Simple is my very best friend :)

    Adding the following to your code...

    use Data::Dumper::Simple; # your code here... print Dumper($res);

    ...yields the following result on my Win32 machine...

    $res = bless( { 'Count' => undef, 'Security_' => bless( { 'ImpersonationLevel' => 3, 'AuthenticationLevel' => 6, 'Privileges' => bless( { 'Coun +t' => 1 }, 'Win +32::OLE' ) }, 'Win32::OLE' ) }, 'Win32::OLE' );

    So I guess the resulting data structure is a little more complex than just a simple hash. No smartdrive info there of course (because I'm not running any), but I suspect that if you try the same thing on your machine you might find the clue that you need.

    Hope this helps,
    Darren :)

      Thanks for the reply, but Dumper doesn't provide me with anything useful; if I replace the last part of the code with
      my $DriveCollection = $WMIServices->InstancesOf("Win32_DiskDrive" ); foreach my $Drive ( in( $DriveCollection ) ) { print Dumper $Drive; print $Drive->{'Status'}; }
      I get a huge dump of a data structure and then the text 'OK' -the property 'Status' is documented in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_diskdrive.asp but not hinted at anywhere in the Dumper output. I can't find a corresponding page for querying SMART info, hence my question.
        Well, again let me point out that I know next to nothing about WMI, so I could be very well just blowing air out my rear-end here. However, looking at that "link" you gave I see the following:
        For IDE, this polling consists of sending the READ_SMART_STATUS comman +d. If this status indicates an error, a WMI event will be launched th +at includes the SMART attribute data.

        It doesn't actually specify what is returned in the absence of an error. Your reply infers that print $Drive->{'Status'}; produces 'OK' ...could it be that this is the entirety of the status?

        Cheers,
        Darren :)