[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 739
  • Last Modified:

How to skip entry when error found via wmi

I have the following perl wmi script:

use Win32::OLE qw(in);
$Win32::OLE::Warn = 3;
print "\n\n\n";
# ------ SCRIPT CONFIGURATION ------
$all_host="host.txt";
open(HOST_TABLE,"<$all_host") || die "Can't open infile: $!\n";
@hostnames = <HOST_TABLE>;
close (HOST_TABLE);

foreach $strComputer (@hostnames)  {
chomp $strComputer;



$objWMI = Win32::OLE->GetObject("winmgmts://$strComputer/root/cimv2");

#if ($objWMI =~ /error/gi) {next;}


print "obi is $objwmi\n\n";
$colNAs = $objWMI->InstancesOf('Win32_NetworkAdapter');
foreach my $objNA (in $colNAs) {

if( $objNA->AdapterType =~ "Ethernet") {
 print "$strComputer ";  
 print $objNA->Name, "\t";
 
    print 'Conn Status:  ' . $objNA->NetConnectionStatus, "\n";
    print "\n";



}
 }}
I need to know how to skip over a compter name is an error is return, ie access is denied and go on to the next server. The followin gis an error I receive from the first entry in my host.txt file.

Win32::OLE(0.1703) error 0x80070005: "Access is denied"
    after character 0 in "winmgmts://TESTSERVER/root/cimv2" at status.pl line 16
        eval {...} called at status.pl line 16
0
MatthewF
Asked:
MatthewF
  • 2
  • 2
  • 2
  • +2
1 Solution
 
Adam314Commented:
What is line 16 in your code?
0
 
MatthewFAuthor Commented:
I am actually getting the following error, the last error was my mistake

Can't call method "Instancesof" on an undefined value at status.pl line 18

line 18 is
$colNAs = $objWMI->InstancesOf('Win32_NetworkAdapter');
0
 
Perl_DiverCommented:
my guess is $objWMI is not getting defined here:

$objWMI = Win32::OLE->GetObject("winmgmts://$strComputer/root/cimv2");


so maybe:

$objWMI = Win32::OLE->GetObject("winmgmts://$strComputer/root/cimv2");
next unless $objWMI;


side note:

you have this:

print "obi is $objwmi\n\n";

when it should be:

print "obi is $objWMI\n\n";



0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Perl_DiverCommented:
actually you should probably declare $objWMI with 'my' :

my $objWMI = Win32::OLE->GetObject("winmgmts://$strComputer/root/cimv2");
next unless $objWMI;

and you should be using "strict" too. But packaging the variable with "my" should insure an old value is not carried into the next loop and causing a problem.
0
 
MatthewFAuthor Commented:
neither one of those considerations worked
0
 
Adam314Commented:
Try adding these 2 lines to the top:
    use strict;
    use warnings;

This will give you extra warning messages to help figure out what is wrong.  Also, include this module:
    use Data::Dumper;


Then after this line:
$objWMI = Win32::OLE->GetObject("winmgmts://$strComputer/root/cimv2");
print Data::Dumper($objWMI);



what output do you get?
0
 
tone28Commented:
You will need to turn off
$Win32::OLE::Warn = 3;

So just omit it or turn it to 0

$Win32::OLE::Warn = 0;

This will stop your code dead even if you can get by an error.

Then Perl_Diver's suggestion

$objWMI = Win32::OLE->GetObject("winmgmts://$strComputer/root/cimv2");
next unless $objWMI;

would work perfect.


0
 
tone28Commented:
Oh another suggesion would be to check

Win32::OLE::LastError()

If(Win32::OLE::LastError()) {
     # Deal with the error or
     Win32::OLE::LastError(0); # Resets the error to 0
     next;
}
0
 
rmaz410Commented:
Try using the eval block to effectively catch the error and continue on:

------------------------------------------------------

#!/usr/bin/perl -w
use strict;

use Win32::OLE qw(in);
$Win32::OLE::Warn = 3;

print "\n\n\n";
# ------ SCRIPT CONFIGURATION ------
$all_host="host.txt";
open(HOST_TABLE,"<$all_host") || die "Can't open infile: $!\n";
@hostnames = <HOST_TABLE>;
close (HOST_TABLE);

foreach $strComputer (@hostnames)  {
  # using the eval block, any error not caught will jump to our error-handler below
  eval {
    chomp $strComputer;
    $objWMI = Win32::OLE->GetObject("winmgmts://$strComputer/root/cimv2");

    print "obi is $objwmi\n\n";
    $colNAs = $objWMI->InstancesOf('Win32_NetworkAdapter');

    foreach my $objNA (in $colNAs) {
      if( $objNA->AdapterType =~ "Ethernet") {
        print "$strComputer ";  
        print $objNA->Name, "\t";
   
        print 'Conn Status:  ' . $objNA->NetConnectionStatus, "\n";
        print "\n";
      }
    }
  };  # be sure to use the semi-colon here at the end of an eval

  # we get here if there was an error in the above eval block
  if ($@) {
    print "Warning: Error querying object $strComputer: $@\n";
    next;
  }
}
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now