####################################################################
# Description: Control services and restart servers in the domain #
####################################################################
use Win32;
use Win32::Service;
print "Enter the server name to check: ";
$Server = (<STDIN>);
chomp $Server;
$Server =~ tr/a-z/A-Z/;
@ServiceList = Status($Server);
$ServiceList = @ServiceList;
if($ServiceList == 0){print "Can't connect to $Server\.\n"};
Menu();
$_ = "Start";
while (!$_ == ""){
$_ = (<STDIN>);
# Options menu
SWITCH:{
# Stop/Start service
if(/1/){
print "Enter the Service num\. you want to Stop/start: ";
$Num = (<STDIN>);
while ($Num > $ServiceList){
print "Wrong Num\. try again: ";
$Num = (<STDIN>);
}
Stop_Start($Server, $ServiceList[$Num]);
last SWITCH;
}
# Check services status again
if(/2/){@ServiceList = Status($Server),$ServiceList = @ServiceList, last SWITCH;}
# Shutdown the server
if(/3/){Shutdown($Server), last SWITCH;}
# Chose another Server
if(/4/){
print "Enter the server name to check: ";
$Server = (<STDIN>);
chomp $Server;
$Server =~ tr/a-z/A-Z/;
@ServiceList = Status ($Server);
$ServiceList = @ServiceList;
if($ServiceList == 0){print "Can't connect to $Server\.\n"};
}
# quit
if(/5/){exit}
}
Menu();
}
#**********************************************************************
#Get services status on a given server
sub Status
{
my $Server = shift ;
my ($key, $services, %status, $ID, @MyServices );
print "\nServices status on $Server:\n";
print "========================\n";
Win32::Service::GetServices($Server,\%services);
$ID = 0;
foreach $key (sort keys %services){
Win32::Service::GetStatus( $Server, $services{$key}, \%status);
print "$ID $key ($services{$key})";
if($status{CurrentState} eq "4"){
print "- Start\n";
}else{ print "- Stop\n"};
push @MyServices, $services{$key};
$ID++;
}
return @MyServices;
}
#*********************************************************************
# Stop/Start service
sub Stop_Start
{
my ($Server, $Service) = @_ ;
my %status;
Win32::Service::GetStatus($Server, $Service, \%status);
if($status{CurrentState} eq "4"){
print "Stop $Service\.\.\n";
Win32::Service::StopService($Server, $Service);
}
else{
print "Start $Service\.\.\n";
Win32::Service::StartService($Server, $Service);
}
sleep 5;
Win32::Service::GetStatus($Server, $Service, \%status);
if($status{CurrentState} eq "4"){print "$Service is Start\n"}
else {print "$Service is Stop\n"};
}
#********************************************************************
# Restars server
sub Shutdown
{
my $Server = shift ;
Win32::InitiateSystemShutdown ($Server,'hope you saved you work!',30,0,1)if ($Computer ne 'q')|| die " \n";
print "$Server will restart in 30 sec\n";
print "You can stop the restart by writing cancel\n";
print "or anything else to back to main menu\.\n";
$act = (<STDIN>);
chop $act;
Win32::AbortSystemShutdown ($Server)if ($act eq 'cancel');
}
#********************************************************************
sub Menu
{
print "\nEnter your choice:\n";
print "1\. Stop/Start service\.\n";
print "2\. Check status again\.\n";
print "3\. Restart the server\.\n";
print "4\. Chose another Server\.\n";
print "5\. quit\.\n";
}