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);
-
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.
|