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 );
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|