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

Hi,
at work I need to get something working under windows 2k and following the instructions in this guide
http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov04/hey1104.mspx
well, it does not work :(. Under xp it does, but upgrading is no option. So I thought, let's try it in perl instead.
this is what I got so far:
--------------------------script--------------- #!/usr/bin/perl use warnings; use strict; use Win32::OLE; my $WMIServices; my $Namespace = "\\root\\cimv2"; my $Machine = "."; my @files; my $strdate = "20031102000000.000000+000"; my $query = "Select * From CIM_DataFile Where CreationDate < '$strdate +' and drive = 'C:'"; print $query . "\n"; my $object = "winmgmts:\\\\$Machine$Namespace"; print $object . "\n"; $WMIServices = Win32::OLE->GetObject("$object"); @files = $WMIServices->ExecQuery("$query"); foreach (@files) { print "$_\n"; }
------------------------------------------------------ and this is what I get:
H:\scripts\perl\win32> perl wmi2.pl Select * From CIM_DataFile Where CreationDate < '20031102000000.000000 ++000' and drive = 'C:' winmgmts:\\.\root\cimv2 Win32::OLE=HASH(0x1986408)
Apparently I get the wmi statement ok, and the object too, but what under vbs works fine, under perl does not. I must be the cause, but I do not see it. Any ideas? thanks in advance

Replies are listed 'Best First'.
Re: win32 wmi question
by Corion (Patriarch) on Nov 16, 2006 at 22:59 UTC

    Under VB, the "default property" is what gets printed. Luckily, Win32::OLE does not overload stringification for the default property, even though that would be convenient at times. Getting at the default property is done the following way:

    print $_->Invoke(''), "\n";

    I find it less confusing to be more explicit with OLE objects. What you really want from the file object is the name:

    print $_->{Name}, "\n";

    I haven't tested this code, but it should work...