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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.