trplebeam1 has asked for the wisdom of the Perl Monks concerning the following question:

Oh Great ones,

I need help with creating a Perl script that will utilize the net stop/start command in a Windows 2000 environment. This script needs to stop and start particular services on a server. (ie SNMP, Messenger, etc)

It will be used as a preoffline script and a postoffline script.

Can anyone help me???

janitored by ybiC: Retitle from "Need script using net stop/start for Windows services"

Replies are listed 'Best First'.
Re: Stop/start Windows services with perl
by TheFluffyOne (Beadle) on Nov 21, 2003 at 18:42 UTC

    I have a script that uses the Win32::Service module to start a service, and it's fairly straightforward to change it to a stop instead. I find it's easier to deal with return codes from a module than a shelled-out "net start" or "net stop".

    Have snipped out the important bit:

    Update: Realised the case of $ServiceName was incorrect.

    Hope that helps...

    Gordon.

      Gordon,

      The application that will utilize this script specifies using only net stop/start commands.

      Thanks for the info though

      Kris

        Hmmm... well in that case, I would use something like:

        @rc = `net stop messenger`; if ($rc[1] =~ /success/) { print "Messenger stopped successfully\n"; } else { print "Problem occurred stopping Messenger\n"; } @rc = `net start messenger`; if ($rc[1] =~ /success/) { print "Messenger started successfully\n"; } else { print "Problem occurred starting Messenger\n"; }

        You might also want to run `net start` on its own first and search for the name of the service you're about to start or stop -- that way you'll know whether it's running.

        Gordon