#  ServiceStatus.pl
#  Example 8.3:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script displays the status of various services.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Win32::Service;

%STATE = (
    0   =>  'unknown',
    1   =>  'stopped',
    2   =>  'starting',
    3   =>  'stopping',
    4   =>  'running',
    5   =>  'resuming',
    6   =>  'pausing',
    7   =>  'paused',
);
              
$DisplayName = shift @ARGV || die;
$Machine = Win32::NodeName() unless( $Machine = shift @ARGV );
$Machine = "\\\\$Machine";
$Machine =~ s/^\\{2,}/\\\\/;
if( Win32::Service::GetServices( $Machine, \%List ) )
{
    if( defined $List{$DisplayName} )
    {
        if( Win32::Service::GetStatus( $Machine, $List{$DisplayName}, \%Status ) )
        {
            print "The $DisplayName service on $Machine is " . $STATE{$Status{CurrentState}} . "\n";
        }
        else
        {
            print "Could not find the '$DisplayName' service. Names are case sensitive.\n";
        }
    }
}
else
{
    print "Could not connect to $Machine: ";
    print Win32::FormatMessage( Win32::GetLastError() ), "\n";
}

