This is a bit out there, but here it goes. I have been using ActiveState perl to query the WMI interface to gather windows hardware info for some time. I am in the process of expanding my reach to linux and mac hardware, but running into an issue. I have been able to steer my code into OS specific sections with if ($^O =~ /MSWin32/i).
I am trying to get the win32 modules pulled into that section and have been successful with most of the modules (Win32::TieRegistry, Win32::NetAdmin, Win32), but Win32::OLE is resisting my efforts.
If I place use Win32::OLE qw(in) before the steering code (available to all OS's), everything works well under windows, but when I run the script on linux where I get can't locate Win32/OLE.pm (as expected). If I place require Win32::OLE::->import (qw(in)) within the Win32 section and comment it out of the main branch, I get the error:
Win32::OLE(0.1712): GetOleEnumObject() Not a Win32::OLE::Enum object at C:/Perl64/lib/Win32/OLD/Lite.pm line 167.
When I look at the documentation, I get a bit confused. Evidently "in" is a function that is exported by Win32::OLE to deal with collections of objects, but when I use require, it isn't being exported correctly. I can include some actual code that works under windows but throws the module missing error under linux...
Thanks for any help!
#!/usr/bin/perl use strict; use warnings; use Win32::OLE qw(in); ### Need to move to Win32 specific section... our ($Registry,$WMI); if ($^O =~ /MSWin32/i) { require Win32; Win32::->import(); require Win32::TieRegistry; Win32::TieRegistry::->import(Delimiter = +>'/'); # require Win32::OLE; Win32::OLE::->import(qw(in)); ### Doesn't work! $WMI = Win32::OLE->GetObject('winmgmts:{impersonationLevel=impersona +te}!\\\\.\\root\\CimV2'); unless (defined $WMI) {Death("Can't connect to WMI interface!");} $WMI->Security_->Privileges->AddAsString('SeLoadDriverPrivilege', 1) +; } elsif ($^O =~ /Linux/i) { # linux things } else {die("Unknown Operating System: <$^O>");} my $ram; my $i = 1; my %inv; foreach my $item (in $WMI->ExecQuery('Select * from Win32_PhysicalMemo +ry')) { my $bank = int($item->Capacity/1024/1024); my $spd = $item->Speed; next unless $spd && $bank; $inv{"Dimm_$i"} = "$bank Mb $spd MHz"; $ram += $bank; $i++; } $inv{"Ram_Installed"} = "$ram Mb" if $ram; foreach my $key (sort keys %inv) {print "$key: \t$inv{$key}\n";} exit;
In reply to Odd WMI situation by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |