I should have realized I can call this from a batch file, my bad. re-adding
use Win32::OLE qw( in ); use strict; $Machine = "." unless( $Machine = shift @ARGV ); $Machine =~ s#^[\\/]+## if( $ARGV[0] =~ m#^[\\/]{2}# ); # This is the WMI moniker that will connect to a machine's # CIM (Common Information Model) repository $CLASS = "WinMgmts:{impersonationLevel=impersonate}!//$Machine"; # Get the WMI (Microsoft's implementation of WBEM) interface $WMI = Win32::OLE->GetObject( $CLASS ) || die "Unable to connect to \\$Machine:" . Win32::OLE->LastError(); # Get the collection of Win32_Process objects $ProcList = $WMI->InstancesOf( "Win32_Process" ); # Try to kill each PID passed in foreach $Pid ( @ARGV ) { my $KillList = GetProc( $Pid, $ProcList ); if( scalar @$KillList ) { foreach my $Proc ( @$KillList ) { print "Killing $Proc->{Name} ($Proc->{ProcessID})..."; # We found the correct Win32_Process object so # call its Terminate method $Result = $Proc->Terminate( 0 ); if( 0 == $Result ) { print "Successfully terminated"; } else { print "Failed to terminate"; } print "\n"; } } else { print "$Pid not found."; } } sub GetProc { my( $Pid, $ProcList ) = @_; if( $Pid =~ /^\d+$/ ) { $List = GetProcByPid( $Pid, $ProcList ); } else { $List = GetProcByName( $Pid, $ProcList ); } return( $List ); } sub GetProcByPid { my( $Pid, $ProcList ) = @_; # Cycle through each Win32_Process object # until you find the right one foreach my $Proc ( in( $ProcList ) ) { return( [ $Proc ] ) if( $Pid == $Proc->{ProcessID} ); } return( [] ); } sub GetProcByName { my( $Name, $ProcList ) = @_; my( @Procs ); my( $Regex ) = ( $Name =~ m#[$^\\/*?{}\[\]]+# ); # Cycle through each Win32_Process object # until you find the right one foreach my $Proc ( in( $ProcList ) ) { if( $Regex ) { push( @Procs, $Proc ) if( $Proc->{Name} =~ /$Name/i ); } else { push( @Procs, $Proc ) if( lc $Name eq lc $Proc->{Name} ); } } return( \@Procs ); }

In reply to kill process by grashoper

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.