http://qs1969.pair.com?node_id=20483

This is a simple test to see the status of WIN32 services. On Windows NT4. I have to give barndoor credit for the WIN32::Service reference because today is the first time I saw it. After playing with it, I figured I would try to get all services and their status. One problem: Some services don't seem to return a CurrentState value. I decided to ignore these and only stick with the set that barndoor has provided.
use Win32::Service; #set up a hash of known service states my %statcodeHash = ( '1' => 'stopped.', '2' => 'start pending.', '3' => 'stop pending.', '4' => 'running.', '5' => 'continue pending.', '6' => 'pause pending.', '7' => 'paused.' ); my %serviceHash; #go get 'em... Win32::Service::GetServices("", \%serviceHash); foreach $key(keys %serviceHash){ my %statusHash; Win32::Service::GetStatus("", "$key", \%statusHash); if ($statusHash{"CurrentState"} =~ /[1-7]/){ print $serviceHash{"$key"} . " is currently " . $statcodeHas +h{$statusHash{"CurrentState"}} . "\n"; } }

Replies are listed 'Best First'.
RE: Simple WIN32 Service Test
by barndoor (Pilgrim) on Jun 30, 2000 at 14:43 UTC
    Under this (and I guess all the WIN32:: stuff) are of course the Windows APIs. If you really want to get into all this stuff and play with Windows internals, then an MSDN subscription is well worth the money. (Before you ask, no I don't work for M$). I pulled all the return codes from the service control API's and there's a lot more info in there too if you need it. The WIN32::Eventlog stuff is very useful too. I've used it to scan machines for certain errors and to log important messages centrally for my scripts. Although this module doesn't seem to be able to read logs on other machines it can be used to log failures in your scripts which can be them checked centrally by the administrator using the standard OS tools.
Re: Simple WIN32 Service Test
by frag (Hermit) on Oct 18, 2001 at 00:06 UTC
    There's an error in your code. The line:
    Win32::Service::GetStatus("", "$key", \%statusHash);
    should be:
    Win32::Service::GetStatus("", $serviceHash{$key}, \%statusHash);

    -- Frag.
    --
    "Just remember what ol' Jack Burton does when the earth quakes, the poison arrows fall from the sky, and the pillars of Heaven shake. Yeah, Jack Burton just looks that big old storm right in the eye and says, "Give me your best shot. I can take it."

      Thanks, frag ,

      I tried your solution and it gives me a much more complete list. Here's the proof:

      Output before the change:

      ANSYS FLEXlm license manager is currently stopped. Norton Program Scheduler is currently running. NAV Alert is currently running. UPS is currently stopped. NAV Auto-Protect is currently running. Spooler is currently running. Flexlm License Server for VPI is currently stopped. SMS Hardware Inventory Agent Service is currently stopped. Messenger is currently running. Retrospect Client is currently running. EventLog is currently running. MySql is currently running. Alerter is currently stopped.
      Output after the change:
      Replicator is currently stopped. ANSYS FLEXlm license manager is currently stopped. WinMgmt is currently running. SENS is currently running. LanmanWorkstation is currently running. Schedule is currently running. Norton Program Scheduler is currently running. Tb2RCAssist is currently running. PlugPlay is currently running. NAV Alert is currently running. NtLmSsp is currently running. LmHosts is currently running. NetDDEdsdm is currently stopped. UPS is currently stopped. ProtectedStorage is currently running. NAV Auto-Protect is currently running. NetDDE is currently stopped. WPMS is currently running. Tb2Launch is currently running. Spooler is currently running. DHCP is currently running. LanmanServer is currently running. Netlogon is currently running. JTAGServer is currently stopped. Browser is currently stopped. Flexlm License Server for VPI is currently stopped. RPCLOCATOR is currently running. TapiSrv is currently stopped. MSIServer is currently stopped. SMS Hardware Inventory Agent Service is currently stopped. Messenger is currently running. ClipSrv is currently stopped. Retrospect Client is currently running. EventLog is currently running. SimpTcp is currently running. clisvc is currently running. EventSystem is currently running. MySql is currently running. RpcSs is currently running. winvnc is currently running. Alerter is currently stopped.

      Mick