Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl - How to retrieve the Status of NT Services 1

Status
Not open for further replies.

dietmarp

Programmer
Dec 27, 2001
53
US
Hi,

how can I use perl to detect the status of NT Services.
e.g SERVICE_RUNNING, SERVICE_STOPPED.
Can someone please provide a sample script.

Rdgs
Dietmar
 
My beginner code is perhaps laughable but the following works fantastically well for us:

# PROGRAM
# subQueryServiceState.pl

# PURPOSE
# returns the state of each queried service

# DEPENDENCIES
# 1) ActiveState perl,
# AUTHOR
# Daniel Burrow, Alerio Corporation, 9/22/2001 8:19PM


# Command line syntax
# perl -w subQueryServiceState.pl


use Win32::Lanman;
use strict;

my $TestServer = 'YourHostName';
my $TestService = 'YourServiceName';
my $DebugReturn = &queryservicestate($TestServer, $TestService);
print "\nDebugReturn: " . $DebugReturn . "\n";


sub queryservicestate {

use strict;

my ($inServer, $inService) = @_;

print "\ninServer: " . $inServer . " inService: " . $inService . "\n";

my (%Status, $Key, $tmpState, $TextState);

my %State = (
1 => 'not started',
2 => 'starting',
3 => 'stopping',
4 => 'running',
5 => 'continuing',
6 => 'pausing',
7 => 'paused' );

if(!Win32::Lanman::QueryServiceStatus("\\\\$inServer", "", $inService, \%Status)) {
print "Sorry, something went wrong; error: ";
# get the error code
print Win32::Lanman::GetLastError();
};

for $Key (sort keys %Status) {
print "$Key = $Status{$Key}\n";
if ($Status{'state'}) {
$tmpState = $Status{'state'};
$TextState = $State{$tmpState};
};
};

print "\n\t$inServer $inService service is: $TextState \n\n";

return $TextState;

};
 
Beginner heck - that's good. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top