Since this is Windows, you can use WMI to start,monitor, and kill the process.
use strict;
use Win32::OLE qw( in );
use Win32::OLE::Variant;
usage if @ARGV < 2;
if( my $Pid = RemoteExec( @ARGV ) ) {
print "Process created with PID $Pid\n";
} else {
print "Process not created\n";
}
exit;
#####################################################################
sub RemoteExec {
my $Machine = shift;
my $CommandLine = join " ", @_;
my($CLASS, $WMI, $Process, $vPid);
$Machine =~ s|^[\\/]+||;
$CLASS = "WinMgmts:{impersonationLevel=impersonate}!//$Machine";
print "Trying to launch @_ on $Machine\n\n";
$WMI = Win32::OLE->GetObject( $CLASS )
or die "Unable to connect to \\\\$Machine :" . Win32::OLE->Las
+tError();
$Process = $WMI->Get( "Win32_Process" )
or die "Unable to get a Win32_Process :" . Win32::OLE->LastErr
+or();
$vPid = Variant( VT_I4 | VT_BYREF, 0 ); # $vPid (out) PID o
+f the created Process
if( 0 == $Process->Create( $CommandLine, undef, undef, $vPid ) ) {
return $vPid;
} else {
return undef;
}
}
#####################################################################
sub usage {
my($ScriptName) = $0 =~ m/.*\\(.*)$/;
print<<"USAGE";
Usage : $ScriptName [\\\\]COMPUTERNAME CommandLine Parameters
example :$ScriptName COMPUTER1 NOTEPAD C:\\FooBar.txt
USAGE
exit;
}
Original node (89593)
To kill the process use kill($Pid);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|