Service Status

Hi guys!

I have a script Id like to write that could do the following.

checkservice.pl -s <servicename> -f <filename>
where:
<servicename> is the name of the service
<filename> is the name of a file that holds a list of computers to process.

What it would do is:

a) Read the filename
b) Connect to each machine
c) Run an sc query \\$system name <$servicename> on each computer in the <filename>
d) Write to the terminal window, and the same standard out, to a file called either running.txt, or stopped.txt, or other.txt, depending on the STATE row of the status, as in the following example;

C:\Documents and Settings\shell>sc query wzcsvc

SERVICE_NAME: wzcsvc
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

So, in the above, output to the terminal would be:

ComputerName             $ServiceName                  $Status
eg.
COMPUTER023              wzcsvc                          RUNNING

The above line would also be written to running.txt

So, output to the screen for a list of machines would be:

COMPUTER023              wzcsvc                          RUNNING
COMPUTER023              wzcsvc                          STOPPED
COMPUTER912              wzcsvc                          RUNNING
COMPUTER41                wzcsvc                          PAUSED

The stopped entries go to stopped.txt.
The running entries go to running.txt
Anything else, goes to other.txt

Any help appreciated.
LVL 1
Simon336697Asked:
Who is Participating?
 
Adam314Commented:
In the if and elsif lines, it looks like "my " got added.  Remove that:


      ...
      if($output =~ /STATE.*RUNNING/i) {
            $state=1;
            $msg='RUNNING';
      }
      elsif($output =~ /STATE.*STOPPED/) {
            $state=2;
            $msg='STOPPED';
      }
      ...

0
 
Adam314Commented:
use Getopt::Long;

my ($ServiceName, $FileName);
GetOptions("s=s" => \$ServiceName,
           "f=s" => \$FileName) or die "Invalid options\n";

die "Usage: $0 -s <ServiceName> -f <FileName>\n" unless (defined($ServiceName) and defined($FileName));

open(IN,"<$FileName") or die "Could not open $FileName: $!\n";
open(OUTRUN, ">running.txt") or die "Could not open running: $!\n";
open(OUTSTOP, ">stopped.txt") or die "Could not open stopped: $!\n";
open(OUTOTHER, ">other.txt") or die "Could not open other: $!\n";

while(<IN>) {
      chomp;
      my $output = `sc query \\\\$_ $ServiceName`;
      my $msg='OTHER';
      my $state=0;
      if($output =~ /STATE.*RUNNING/) {
            $state=1;
            $msg='RUNNING';
      }
      elsif($output =~ /STATE.*STOPPED/) {
            $state=2;
            $msg='STOPPED';
      }
      print OUTOTHER "$_\n" if $state==0;
      print OUTSTOP "$_\n" if $state==2;
      print OUTRUN "$_\n" if $state==1;
      print "$_\t$ServiceName\t$msg\n";
}
close(IN);
close(OUTSTOP);
close(OUTRUN);
close(OUTOTHER);
0
 
Simon336697Author Commented:
God youre so good Adam!
I mean that as well, not being sarcastic.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Simon336697Author Commented:
Adam my mistake.

The line

my $output = `sc query \\\\$_ $ServiceName`;

should be

my $output = `sc  \\\\$_ query $ServiceName`;

as the below shows..

C:\>sc \\predator query browser

SERVICE_NAME: browser
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

=======================================================

The current way gives:


C:\>sc query \\predator browser
[SC] EnumQueryServicesStatus:OpenService FAILED 123:

The filename, directory name, or volume label syntax is incorrect.

=========================================================== Updated Script:

use Getopt::Long;

my ($ServiceName, $FileName);
GetOptions("s=s" => \$ServiceName,
           "f=s" => \$FileName) or die "Invalid options\n";

die "Usage: $0 -s <ServiceName> -f <FileName>\n" unless (defined($ServiceName) and defined($FileName));

open(IN,"<$FileName") or die "Could not open $FileName: $!\n";
open(OUTRUN, ">running.txt") or die "Could not open running: $!\n";
open(OUTSTOP, ">stopped.txt") or die "Could not open stopped: $!\n";
open(OUTOTHER, ">other.txt") or die "Could not open other: $!\n";

while(<IN>) {
      chomp;
      my $output = `sc \\\\ query $_ $ServiceName`;
      my $msg='OTHER';
      my $state=0;
      if($output =~ /STATE.*RUNNING/) {
            $state=1;
            $msg='RUNNING';
      }
      elsif($output =~ /STATE.*STOPPED/) {
            $state=2;
            $msg='STOPPED';
      }
      print OUTOTHER "$_\n" if $state==0;
      print OUTSTOP "$_\n" if $state==2;
      print OUTRUN "$_\n" if $state==1;
      print "$_\t$ServiceName\t$msg\n";
}
close(IN);
close(OUTSTOP);
close(OUTRUN);
close(OUTOTHER);

========================================================================

When the above updated script is ran, choosing the browser service as an example, which is started, I get:


C:\>service.pl -s browser -f sys.txt
predator        browser OTHER


If I put in a bogus name for a service, I get the same as if I put in a valid service.

C:\>service.pl -s goo -f sys.txt
predator        goo     OTHER

0
 
Simon336697Author Commented:
Sorry again.

my $output = `sc \\$_ query $ServiceName`;

0
 
Simon336697Author Commented:
I must be getting tired...take 3:

my $output = `sc \\\\$_ query $ServiceName`;

0
 
Adam314Commented:
So with the latest
    my $output = `sc \\\\$_ query $ServiceName`;
does it work as expected?
0
 
Simon336697Author Commented:
Adam!

With:
 my $output = `sc \\\\$_ query $ServiceName`;

It still defaults to going to OTHER, even tho the service is started.

If i run it from the command line:

sc \\predator query browser
it works fine, so the $output is syntactically correct.

Im wondering if its because of the matching criteria
if($output =~ /STATE.*RUNNING/) {

because the output when run from a command line looks multiline, with whitepace in between.


C:\>sc \\predator query browser

SERVICE_NAME: browser
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

C:\>
0
 
Adam314Commented:
with STATE and RUNNING on the same line, it would still match.

After the my $output = `sc \\\\ query $_ $ServiceName`; line,put this:
print "Output=$output\n";

what is the output?
0
 
Simon336697Author Commented:
Adam, here is the output with the print line placed in.....


C:\>service.pl -s browser -f sys.txt
Output=
SERVICE_NAME: browser
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

predator        browser OTHER
0
 
Simon336697Author Commented:
Just to recap...here is the code Im using again.....

================================================================
use Getopt::Long;

my ($ServiceName, $FileName);
GetOptions("s=s" => \$ServiceName,
           "f=s" => \$FileName) or die "Invalid options\n";

die "Usage: $0 -s <ServiceName> -f <FileName>\n" unless (defined($ServiceName) and defined($FileName));

open(IN,"<$FileName") or die "Could not open $FileName: $!\n";
open(OUTRUN, ">running.txt") or die "Could not open running: $!\n";
open(OUTSTOP, ">stopped.txt") or die "Could not open stopped: $!\n";
open(OUTOTHER, ">other.txt") or die "Could not open other: $!\n";

while(<IN>) {
      chomp;
      my $output = `sc \\\\$_ query $ServiceName`;
      print "Output=$output\n";
     
      my $msg='OTHER';
      my $state=0;
      if(my $output =~ /STATE.*RUNNING/i) {
            $state=1;
            $msg='RUNNING';
      }
      elsif(my $output =~ /STATE.*STOPPED/) {
            $state=2;
            $msg='STOPPED';
      }
      print OUTOTHER "$_\n" if $state==0;
      print OUTSTOP "$_\n" if $state==2;
      print OUTRUN "$_\n" if $state==1;
      print "$_\t$ServiceName\t$msg\n";
}
close(IN);
close(OUTSTOP);
close(OUTRUN);
close(OUTOTHER);

===================================================================
0
 
Simon336697Author Commented:
Adam could it be because the dot in
/STATE.*RUNNING/i) {

matches any character, but not a newline, and this is multiline?
0
 
Simon336697Author Commented:
GENIUS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

------------------- Successful code tests:

use Getopt::Long;

my ($ServiceName, $FileName);
GetOptions("s=s" => \$ServiceName,
           "f=s" => \$FileName) or die "Invalid options\n";

die "Usage: $0 -s <ServiceName> -f <FileName>\n" unless (defined($ServiceName) and defined($FileName));

open(IN,"<$FileName") or die "Could not open $FileName: $!\n";
open(OUTRUN, ">running.txt") or die "Could not open running: $!\n";
open(OUTSTOP, ">stopped.txt") or die "Could not open stopped: $!\n";
open(OUTOTHER, ">other.txt") or die "Could not open other: $!\n";

while(<IN>) {
      chomp;
      my $output = `sc \\\\$_ query $ServiceName`;
      #print "Output=$output\n";
     
      my $msg='OTHER';
      my $state=0;
      if($output =~ /STATE.*RUNNING/) {
            $state=1;
            $msg='RUNNING';
      }
      elsif($output =~ /STATE.*STOPPED/) {
            $state=2;
            $msg='STOPPED';
      }
      print OUTOTHER "$_\n" if $state==0;
      print OUTSTOP "$_\n" if $state==2;
      print OUTRUN "$_\n" if $state==1;
      print "$_\t$ServiceName\t$msg\n";
}
close(IN);
close(OUTSTOP);
close(OUTRUN);
close(OUTOTHER);

=============================================== Script:

use Getopt::Long;

my ($ServiceName, $FileName);
GetOptions("s=s" => \$ServiceName,
           "f=s" => \$FileName) or die "Invalid options\n";

die "Usage: $0 -s <ServiceName> -f <FileName>\n" unless (defined($ServiceName) and defined($FileName));

open(IN,"<$FileName") or die "Could not open $FileName: $!\n";
open(OUTRUN, ">running.txt") or die "Could not open running: $!\n";
open(OUTSTOP, ">stopped.txt") or die "Could not open stopped: $!\n";
open(OUTOTHER, ">other.txt") or die "Could not open other: $!\n";

while(<IN>) {
      chomp;
      my $output = `sc \\\\$_ query $ServiceName`;
      #print "Output=$output\n";
     
      my $msg='OTHER';
      my $state=0;
      if($output =~ /STATE.*RUNNING/) {
            $state=1;
            $msg='RUNNING';
      }
      elsif($output =~ /STATE.*STOPPED/) {
            $state=2;
            $msg='STOPPED';
      }
      print OUTOTHER "$_\n" if $state==0;
      print OUTSTOP "$_\n" if $state==2;
      print OUTRUN "$_\n" if $state==1;
      print "$_\t$ServiceName\t$msg\n";
}
close(IN);
close(OUTSTOP);
close(OUTRUN);
close(OUTOTHER);

===================================================

WELL DONE ADAM......THANK U AGAIN SO MUCH MATE.
0
 
Simon336697Author Commented:
Sorry code tests again...


C:\>service.pl -s nod32krn -f sys.txt
Output=
SERVICE_NAME: nod32krn
        TYPE               : 110  WIN32_OWN_PROCESS (interactive)
        STATE              : 4  RUNNING
                                (NOT_STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

predator        nod32krn        RUNNING

C:\>service.pl -s netdde -f sys.txt
Output=
SERVICE_NAME: netdde
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 1  STOPPED
                                (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 1077       (0x435)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

predator        netdde  STOPPED

C:\>service.pl -s bogus -f sys.txt
Output=[SC] EnumQueryServicesStatus:OpenService FAILED 1060:

The specified service does not exist as an installed service.


predator        bogus   OTHER

=========================================== And with the output commented.


C:\>service.pl -s bogus -f sys.txt
predator        bogus   OTHER

C:\>service.pl -s netdde -f sys.txt
predator        netdde  STOPPED

C:\>service.pl -s nod32krn -f sys.txt
predator        nod32krn        RUNNING

C:\>
0
 
Adam314Commented:
So is that what you want?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.