#  ServicePause.pl
#  Example 8.2:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script temporarily pauses a service then resumes it.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Win32::Service;
$Timeout = 5;
$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} )
    {
        print "Pausing the $DisplayName service on $Machine.\n";
        Win32::Service::PauseService( $Machine, $List{$DisplayName} );
        print "Sleeping for $Timeout seconds...\n";
        sleep( $Timeout );
        print "Resuming the $DisplayName service on $Machine.\n";
        Win32::Service::ResumeService( $Machine, $List{$DisplayName} );
    }
    else
    {
        print "There is no '$DisplayName' service on $Machine.\n";
    }
}
else
{
    print "Could not connect to $Machine: ";
    print Win32::FormatMessage( Win32::GetLastError() ), "\n";
}

