Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; use Win32::PerfLib; use Win32::Process; use Getopt::Std; use vars qw($opt_d); ################# # # # # kill all processes by a certain name # on windows. # # Run program without parameters # for usage. # &print_usage() unless @ARGV; # Get all process IDs by name. my %process_by_name = &get_processes; # Dump all available processes if -d : getopts('d'); if($opt_d) { print join "\n", sort keys %process_by_name; exit; } # Sort through command line names: foreach my $doomed (@ARGV) { # Sort out matching processes: my @process_id = (map { /^$doomed$/i ? @{$process_by_name{$_}} : ( +) } keys %process_by_name); if(!@process_id) { print "$doomed: no process killed\n"; next; } # Blam! Take that, you lowly process you! Win32::Process::KillProcess($_, 0) foreach(@process_id); } # Retrieves all process ID:s, sorted by name. # Only slightly modified code from the # Win32::PerfLib manpage, actually. sub get_processes { my (%counter, %r_counter, %process_by_name); Win32::PerfLib::GetCounterNames('', \%counter); %r_counter = reverse %counter; # Get id for the process object my $process_obj = $r_counter{'Process'}; # Get id for the process ID counter my $process_id = $r_counter{'ID Process'}; # create connection to local computer my $perflib = new Win32::PerfLib(''); my $proc_ref = {}; # get the performance data for the process object $perflib->GetObjectList($process_obj, $proc_ref); $perflib->Close(); my $instance_ref = $proc_ref->{'Objects'}->{$process_obj}->{'Insta +nces'}; foreach my $instance (values %{$instance_ref}) { my $counter_ref = $instance->{'Counters'}; foreach my $counter (values %{$counter_ref}) { if($counter->{'CounterNameTitleIndex'} == $process_id) { # Process ID:s stored by name, in anonymous array: push @{$process_by_name{$instance->{'Name'}}}, $counte +r->{'Counter'}; } } } return %process_by_name; } sub print_usage { print <<"USAGE"; Usage: $0 file [file_2...file_n] $0 -d Filenames are caseinsensitive, but must otherwise have all letters. So if the process is named 'CMD', you can whack it with 'CMD', 'cmd', or 'CmD' for instance. However, you will not whack 'WINCMD32' this way (a good thing). In the *NIX tradition, when all goes well, the program keeps quiet. It is when it speaks, something is wrong. :) The -d option dumps all the names that are available, if you don't know the exact name of the process you want to whack. USAGE exit; }

In reply to A killall for Windows by Dog and Pony

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (6)
    As of 2018-05-21 10:09 GMT
    Find Nodes?
      Voting Booth?