[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

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.
0
Simon336697
Asked:
Simon336697
  • 10
  • 5
1 Solution
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now