CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
WMI + Arrays and Lists.



Aug 7, 2002, 5:02 AM

Post #1 of 3 (1493 views)
WMI + Arrays and Lists. Can't Post

Hello all.
I have just started usin perl. I writing a script to gather system information using WMI. However for information on a network card it returns a massive bunch of strings and I need only get 2 of these strings.

I have created an array which contains the the strings I need.

my @RETURNS = qw( 3Com 3C905C-TX);

The string which is returned by WMI needs to be assigned to an array (i think).

foreach $Object (in ($WMIServices->InstancesOf( "Win32_NetworkAdapter" ) ) ){
@cardInfo = qw($Object->{Name});
my $Returns = $cardInfo->{Name} || [];
foreach my $Ret ( @{$Returns}) {
print "\t\t$RETURNS[$Ret] || UNKNOWN \n";

So I done something like that, and try and compare, and if the string are equal then print them. If not print UNKNOWN.

Any suggestions would be really cool.


User / Moderator

Aug 7, 2002, 6:32 AM

Post #2 of 3 (1491 views)
Re: [Bode] WMI + Arrays and Lists. [In reply to] Can't Post


I am a little unclear as to what you are trying to do. However, in your code, you
have written:

In Reply To
foreach $Object (in ($WMIServices->InstancesOf( "Win32_NetworkAdapter" ) ) ){
@cardInfo = qw($Object->{Name});
my $Returns = $cardInfo->{Name} || [];
foreach my $Ret ( @{$Returns}) {
print "\t\t$RETURNS[$Ret] || UNKNOWN \n";

It is not clear where the closing bracket '}' is for the first foreach loop.

Also, These errors are apparent from what I understand:

There is no 'in' function in Perl.
Your @cardinfo array will only catch the last network adapter name
You cannot dereference an array as a hash. @ = array $ - can be hashref

So, I think this is what you meant. If not, post back and we can discuss:

foreach $Object (@{$WMIServices->InstancesOf( "Win32_NetworkAdapter" )}){
push @cardInfo, $Object;
}foreach my $card(@cardinfo){
print ("\t\t", ($card->{Name} || "UNKNOWN"), "\n");

Let me know. Hope it helps.

print unpack"u*",pack "h*",$a,"\n\n";


Aug 7, 2002, 8:12 AM

Post #3 of 3 (1483 views)
Re: [Bode] WMI + Arrays and Lists. [In reply to] Can't Post

Cheers for your reply.

Heres the whole script!


use vars qw ($log $message $name $value);
use Win32::OLE qw( EVENTS HRESULT in );

my @RETURNS = qw( 3Com 3C905C-TX );

#Get machine name from command line.
( my $Machine = shift @ARGV || " . " ) =~ s/^[\\\/]+//;

my $WMIServices = Win32::OLE->GetObject( "winmgmts:{impersonationLevel=impersonate,(security)}//$Machine/" ) || die "error connecting to machine $Machine. It maybe switched off.";

#Get Network Card info.
foreach $Object (in ($WMIServices->InstancesOf( "Win32_NetworkAdapter" ) ) ){
$cardName = ($Object->{Name});
print $cardName;

When u run it it returns loads of information - to much to enter it into a database. I have deacred in the array @RETURNS, the 2 strings which i require. So I need to compare this array with the information which is returned, which is the problem I have at the moment. And if there is a match print it.

Hope this is clear.


PS if u want to try this but not got a networkcard change "Win32_NetworkAdapter" for "Win32_POTSModem".


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives