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

Dear Monks, I am using use Win32::Service module to get services status. why it does not print out the service status? it gets my hostname and service name
my ($hostname, $servicename, $entry); my (%status, @array); %status = ( 1 => 'Stopped', 2 => 'START_PENDING', 3 => 'STOP_PENDING', 4 => 'Started', 5 => 'CONTINUE_PENDING', 6 => 'PAUSE_PENDING', 7 => 'PAUSED', 8 => 'ERROR' ); Win32::Service::GetStatus('', $servicename, \%status); print MYFILE " ServiceStatus: ", $hostname, " ", $servicename, " + ", $status{$_}, " ","\n";
Thanks in advance!

Replies are listed 'Best First'.
Re: how to print out windows service status
by choroba (Cardinal) on Jul 31, 2017 at 13:49 UTC
    > $status{$_}

    Where do you set/change the value of $_?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      it is not getting from Win32::Service::GetStatus('', $servicename, \%status); ?
        it is not getting from Win32::Service::GetStatus('', $servicename, \%status); ?

        There is nothing in the documentation to indicate that. Based on the test file this seems to be how to use the module (untested since I'm not on windows at the moment):

        my %services; Win32::Service::GetServices("", \%services); while ( my ($k,$v) = each %services ) { print "$k\t$v\n"; my %status; Win32::Service::GetStatus("", $v, \%status); while ( my ($k,$v) = each %status ) { print "\t$k\t$v\n"; } }

        I'm guessing you want $status{'CurrentState'}. Try

        #!perl use strict; use Win32::Service qw'GetServices GetStatus'; my @state_name = qw( '' Stopped START_PENDING STOP_PENDING Started CONTINUE_PENDING PAUSE_PENDING PAUSED ERROR ); my $hostname = 'localhost'; my %service=(); GetServices($hostname,\%service) or die "$!"; for my $name (sort keys %service){ my $shortname = $service{$name}; my %status = (); # keys to %status # CheckPoint # ControlsAccepted # CurrentState # ServiceSpecificExitCode # ServiceType # WaitHint # Win32ExitCode GetStatus($hostname, $shortname, \%status); my $state_no = $status{'CurrentState'}; printf " ServiceStatus: %s %s %s \n", $hostname, $shortname, $state_name[$state_no]; }
        poj
Re: how to print out windows service status
by thanos1983 (Parson) on Jul 31, 2017 at 13:50 UTC

    Hello ytjPerl,

    I am not running on WindowsOS so I can not really help, but try the sample of code from this similar question Simple WIN32 Service Test.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!