Win32::Service Questions

G

google

I want to create a web-based tool that will allow me to stop and start
a specfic service.

I was able to use the sample code to generate a list of all services
and, for each, its corresponding status.

What I am unable to do is stop or start a service using StopService or
StartService. I am using the translated/hashed/keyed value as the
parameter. For example, instead of using "WebTrends Reporting UI", I
am using "wtinterface", but it still does not work.

I thought it might be permissions inside the web browser (web server
running as System account), but when I run my test script from the
command line as administrator, it doesn't work either. My stop script
is listed below.

Any suggestions?

Thanks!

-peter



use strict;
use warnings;
use Win32::Service qw[GetServices GetStatus StartService StopService];

print "Content-type: text/html\n\n";

my (%status);


print "<STRONG>WebTrends Reporting UI</STRONG><BR><BR>\n";


GetStatus('', 'wtinterface', \%status);
foreach (keys %status)
{
if ($_ eq 'CurrentState')
{
print "The current status is $status{$_}.<BR>\n" ;
}
}


StopService('', 'wtinterface');
print "The service was stopped (I think).<BR>";


GetStatus('', 'wtinterface', \%status);
foreach (keys %status)
{
if ($_ eq 'CurrentState')
{
print "The current status is $status{$_}.<BR>\n" ;
}
}
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g49g2000cwa.googlegroups.com:
I want to create a web-based tool that will allow me to stop and start
a specfic service.

I was able to use the sample code to generate a list of all services
and, for each, its corresponding status.

What I am unable to do is stop or start a service using StopService or
StartService. I am using the translated/hashed/keyed value as the
parameter. For example, instead of using "WebTrends Reporting UI", I
am using "wtinterface", but it still does not work.

I thought it might be permissions inside the web browser (web server
running as System account), but when I run my test script from the
command line as administrator, it doesn't work either. My stop script
is listed below.

I don't know the answer to your question. On my XPSP2 machine with
Apache running as a user process, a simple CGI script to turn Windows
Time service on/off works fine.

However, I do have a few comments about your code below.
GetStatus('', 'wtinterface', \%status);
foreach (keys %status)
{
if ($_ eq 'CurrentState')
{
print "The current status is $status{$_}.<BR>\n" ;
}
}

Do you have the faintest idea why we use hashes rather than arrays?

if( Win32::Service::GetStatus('', 'wtinterface', \%status) ) {
print "The current status is $status{CurrentState}\n";
} else {
die "Cannot obtain status for 'wtinterface' service";
}

My best guess is still some sort of permissions issue. On the other
hand, I do not want to waste time setting up Apache as a service etc.

You might want to try the following script, both from the command line,
and as CGI:

#!/usr/bin/perl

use strict;
use warnings;

use constant SERVICE_DISPLAY_NAME => 'Windows Time';

use Data::Dumper;
use Win32::Service;

use CGI;
$CGI::pOST_MAX = 1;
$CGI::DISABLE_UPLOADS = 1;

$| = 1;
print CGI::header('text/plain');

my $service_name;
my %services;

if( Win32::Service::GetServices('', \%services) ) {
$service_name = $services{SERVICE_DISPLAY_NAME()};
} else {
die 'Cannot retrieve service name for ', SERVICE_DISPLAY_NAME;
}

print "Checking service status for '$service_name' ...\n";

my %status;
if( Win32::Service::GetStatus('', $service_name, \%status) ) {
print Dumper \%status;
} else {
die "Cannot retrieve service status for $service_name";
}

Win32::Service::StopService('', $service_name)
or die "Cannot stop service '$service_name'";

sleep 3;

Win32::Service::GetStatus('', $service_name, \%status)
or die "Cannot get status for '$service_name'";
print Dumper \%status;

Win32::Service::StartService('', $service_name)
or die "Cannot start service '$service_name'";

sleep 3;

Win32::Service::GetStatus('', $service_name, \%status)
or die "Cannot get status for '$service_name'";

print Dumper \%status;

__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top