I need help altering this Perl code. I can get a list of all services on the host. But, I’m only interested in sending an email message for those services that are Red (stopped). I’m not sure where I set that condition (within the foreach, perhaps)?
Output now:
Service: AeLookupSvc - Green - Auto
Service: Alerter - Red - Disabled
Service: ALG - Red - Manual
Service: AppMgmt - Green - Manual
Perl code:
my @servstat = ("Red","Green");
my $computer = "localhost";
my $winsvcs;
my $wmiObj = Win32::OLE->GetObject("winmgmts:\\\\$computer\\root\\CIMV2")
or die "WMI connection failed.\n";
#get all services
my $servSet = $wmiObj->ExecQuery("SELECT * FROM Win32_Service", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
foreach my $serv (in $servSet) {
my $sname = $serv->{name};
my $sstate = $serv->{started};
my $ssmode = $serv->{startmode};
$winsvcs .= "Service: " . $sname . " - " . $servstat[$sstate] . " - " . $ssmode . "\n";
}
You can deal with the condition in the SQL query (only return the rows that are relevant – the Red ones). This is probably the best way of dealing with it; it limits the data sent between DBMS and client (which may not matter much on a single machine, or on a LAN connection, but really matters on a WAN connection).
Alternatively, you can use a test in the
forloop so that you add the relevant information to$winsvcsonly if the$sstateis 0 (equivalent to Red).Note that the
.operator can be necessary (particularly if you need to embed the result of a function call), but wasn’t really needed in the code you showed. You could even do without the 3 loop variables, but then the line becomes rather long.Both methods will work. I recommend modifying the SQL rather than the loop, but the choice is yours.