Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Capture OLE Execption

by vamsinm (Initiate)
on Nov 05, 2012 at 16:45 UTC ( #1002359=perlquestion: print w/replies, xml ) Need Help??
vamsinm has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks, I am trying to write a perl script that captures Serverice status on the servers with a specific username. the script reads the server list from a text file and ping to verify if the server is online and then connects to wmi using SWbemLocator using the admin username. i want to capture when the script fails to connect to wmi and update the excel sheet against the server name. but the script just errors out and stops. can some one please help me how to capture this error and store in the output report.

Error: OLE exception from "SWbemLocator": Access is denied. Win32::OLE(0.1709) error 0x80070005: "Access is denied" in METHOD/PROPERTYGET "ConnectServer" at line 102.

Line 102 :  my $objWMIService = $locatorObj->ConnectServer($host, "root\\cimv2", $user, $pwd);

#use strict; use Net::Ping; use Switch; use warnings; use Net::DNS; use Win32::OLE; use Win32::OLE qw(in with); use Net::Ping; $Win32::OLE::Warn = 3; use Carp; use constant wbemFlagReturnImmediately => 0x10; use constant wbemFlagForwardOnly => 0x20; #my $computer; my $user = "username"; my $pwd = "password"; my $x; my $timeout = 10; my $host = ""; my $Srvstatus = ""; my $Status =""; my $Startmode = ""; my $Comments = ""; # Create a new ping object $pingit = Net::Ping->new("icmp"); # use existing instance if Excel is already running eval {$xlApp = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; my $locatorObj =Win32::OLE->new("WbemScripting.SWbemLocator") or die " +ERROR CREATING OBJ"; $locatorObj->{Security_}->{impersonationlevel} = 3; # Start Excel and make it visible $xlApp = Win32::OLE->new('Excel.Application'); $xlApp->{Visible} = 1; # Create a new workbook $xlBook = $xlApp->Workbooks->Add; $sheet = $xlBook -> Worksheets("Sheet1"); $sheet-> Activate; $sheet->Cells(1,1)->{'Value'} = "servername"; $sheet->Cells(1,2)->{'Value'} = "Service found"; $sheet->Cells(1,3)->{'Value'} = "Status"; $sheet->Cells(1,4)->{'Value'} = "Start Mode"; $sheet->Cells(1,5)->{'Value'} = "comments"; $row = 2; open FILE, "c:\\serverping.txt" or die "could not open the file"; @lines = <FILE>; foreach $line (@lines) { $host = ""; $Srvstatus = ""; $Status =""; $Startmode = ""; $Comments = ""; print $line,"\n"; #Trimming Leading and trailing spaces $line =~ s/^\s+//m; $line =~ s/\s+$//m; $sheet->Cells($row,1)->{'Value'} = $line; $host = $line; # perform the ping if( $pingit->ping($host, $timeout) ) { print "Host ".$host." is alive\n"; #$sheet->Cells($row,4)->{'Value'} = "Host ".$host." is alive"; + #my $result = eval { # $DB->Execute( $SQLquery, 128 ) #}; #unless (defined $result) { # print $ERROR win32_error_message(Win32::OLE->LastError); # Win32::OLE->LastError(0); #} #my $objWMIService = $locatorObj->ConnectServer($host, "root +\\cimv2", $user, $pwd) or die "WMI connection failed.\n", Win32::OLE- +>LastError; my $objWMIService = $locatorObj->ConnectServer($host, "root\ +\cimv2", $user, $pwd); if (Win32::OLE->LastError()) { $sheet->Cells($row,5)->{'Value'} = "$_\n"; $sheet->Cells($row,2)->{'Value'} = "No"; print "$_\n"; Win32::OLE->LastError(0); # this clears your error } else { $colItems = $objWMIService->ExecQuery ("SELECT * FROM Win32_Service Where Name = 'Patrol +Agent'","WQL",wbemFlagReturnImmediately | wbemFlagForwardOnly); foreach my $objItem (in $colItems) { $Srvstatus = $objItem->{Name}; $Status =$objItem->{State}; $Startmode = $objItem->{StartMode}; } print "$Srvstatus \t $Status \t $Startmode \n"; if (lc($Srvstatus) eq 'patrolagent') { $sheet->Cells($row,3)->{'Value'} = $Status; $sheet->Cells($row,4)->{'Value'} = $Startmode; $sheet->Cells($row,2)->{'Value'} = "Yes"; $sheet->Cells($row,5)->{'Value'} = "Yes"; } else { $sheet->Cells($row,5)->{'Value'} = "Could not find + Patrol Service"; $sheet->Cells($row,2)->{'Value'} = "No"; } } } else { #print "Warning: ".$host." appears to be down or icmp packets ar +e blocked by their server\n"; $sheet->Cells($row,5)->{'Value'} = "Warning: ".$host." Down"; $sheet->Cells($row,2)->{'Value'} = "No"; } $row++; } close FILE; $pingit->close(); $xlBook->{Saved} = 1; $xlApp-> {DisplayAlerts} = 0; # This turns off the "This file already +exists" message. $xlBook-> SaveAs ("C:\\temp\\ServiceStatusCheck.xls"); $xlApp-> Quit; $xlBook = 0; $xlApp = 0;

Replies are listed 'Best First'.
Re: Capture OLE Execption
by sundialsvc4 (Abbot) on Nov 05, 2012 at 17:33 UTC

    The exception-handling mechanism in Perl is eval{}.   Also consider Try::Tiny.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002359]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-06-24 11:34 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (557 votes). Check out past polls.