First off, remove all the declarations at the start of the program and only declare variables at the point you actually need them. One of your problems is that various variable (especially $key) don't contain what you hope they will.

You don't need to interpolate variables into strings to do stuff with them. Use $service_name eq $user_selection rather than "$service_name" eq "$user_selection".

A reworked version of your code that seems to work follows:

use strict; use Win32::Console; use Win32::Service; my %service; Win32::Service::GetServices ("", \%service); my @sorted = sort (keys (%service)); my $cons = Win32::Console->new (STD_OUTPUT_HANDLE); my $current_date = localtime (time); my %codes = ( 1 => "Stopped.", 2 => "Starting.", 3 => "Stopping.", 4 => "Running.", 5 => "Continue.", 6 => "Pausing.", 7 => "Paused.", 8 => "Unknown.", 9 => "Unknown." ); while (1) { $cons->Cls (); print "\n\n\n\n"; print "\t" . ('-' x 53) . "\n"; #Menu heading print "\t \t Windows Services Status Viewer\n"; print "\t" . ('-' x 53) . "\n\n"; print "\t 1. Service names started in ascending order.\n"; print "\t 2. Service names stopped in ascending order.\n"; print "\t 3. Exit.\n\n"; print "\t" . ('-' x 53) . "\n"; #Menu heading print "\t \t\t Version 1.0\n"; print "\t \t $current_date\n"; print "\t" . ('-' x 53) . "\n\n"; print "\n\n\t\t Select option (1 to 3) = "; my $option = <STDIN>; if ($option == 1) { started (); } else { if ($option == 2) { stopped (); } else { if ($option == 3) { print "\t" . ('-' x 53) . "\n"; print "\t \t\t Goodbye\n"; print "\t" . ('-' x 53) . "\n"; last; } else { if ($option != [1 - 3]) { print " Please enter a valid a valid option (1 - 3 +) \n "; system (" pause "); } } } } } sub started { foreach my $number (1 .. @sorted) { my $key = $sorted[$number - 1]; my %status; Win32::Service::GetStatus ("", $service{$key}, \%status); if ($status{CurrentState} == 4) { print " \t$number\t $key\n "; } } print " Please enter the number of the service to terminate : \n " +; chomp (my $user_selection = <STDIN>); if (--$user_selection < 0 || $user_selection > $#sorted) { print "Invalid service number\n"; return; } my $key = $sorted[$user_selection]; my %status; Win32::Service::GetStatus ("", $service{$key}, \%status); my $running = $status{CurrentState}; if ($running == 4) { print "$key $codes{$running} and will be stopped \n "; Win32::Service::StopService ("", $key); print "$key has been stopped !\n "; } }

Perl is environmentally friendly - it saves trees

In reply to Re: Stopping Service with Win32::StopService by GrandFather
in thread Stopping Service with Win32::StopService by JasonDavis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.