#!C:/ActivePerl/bin/perl.exe
use strict;
use warnings;
use Win32::OLE qw/in/;
use Getopt::Long;
#
#How to run a process on a remote workstation (NT/2K)
#
my ($remote_computer, $command_path, $help);
GetOptions ('r:s'=>\$remote_computer,
'c:s'=>\$command_path,
'h' =>\$help )
or warn("Couldn't read arguments.\n");
help_and_exit() if ($help);
help_and_exit() if (!defined($remote_computer) || !defined($command_pa
+th) );
my $wmihandle = Win32::OLE->GetObject( "winmgmts:{impersonationLeve
+l=impersonate,(security)}//$remote_computer\\root\\cimv2");
my $wmiprocesses = Win32::OLE->GetObject( "winmgmts:{impersonationLeve
+l=impersonate,(security)}//$remote_computer\\root\\cimv2:Win32_Proces
+s");
#Is the process running already?
unless ( cmd_is_running($wmihandle,$command_path) ) {
print "$command_path is not already running.\n";
}
#This doesn't seem to be necessary but it's in the docs!
#http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmis
+dk/wmi/create_method_in_class_win32_process.asp
my $Startup_Class = $wmihandle->Get("Win32_ProcessStartup");
my $Startup_Config = $Startup_Class->SpawnInstance_ ;
my ($error, $pid, $startup_folder);
$startup_folder="\\\\cntr035533\\c\$\\TEMP\\";
#The Create() method returns 0 on success
if (0 == $wmiprocesses->Create($command_path) ) {
$pid = $wmiprocesses->{'ProcessID'};
if (! $pid) {
$pid = " [PID not available from Win32_Process.ProcessID]";
}
print "Successfully started command with pid $pid\n";
}else{
print "Failed to create process with Win32_Process.Create(cmd)\n";
print "Trying Win32_Process.Create(cmdpath, startup, Win32_ProcessS
+tartup, processid)\n";
$error = $wmiprocesses->Create($command_path,$startup_folder,$Start
+up_Config,$pid);
#Error codes are 'explained' at the msdn link above.
if ($error) {
print "$command_path failed to start with WMI err: $error\n";
+
}else{
print "Started $command_path at 2nd attempt.\n";
}
}
#Check to see that the process is running.
unless ( cmd_is_running($wmihandle,$command_path) ) {
print "$command_path doesn't appear to be running.\n";
}
#--- SUBS ---
sub cmd_is_running{
my ($wmi,$cmd) = @_;
return undef if not defined($wmi);
return undef if not defined($cmd);
my $cmdname;
if ( $cmd=~ /(?:\\|\/)*([^\\\/]+)$/ ) {
$cmdname=uc($1);
}else{
print "Failed to get command name from $cmd\n";
return 0;
}
my $message = '';
#Ask the WMI Class for a list of Processes
foreach my $process (in($wmihandle->InstancesOf("Win32_Process")))
+{
if (uc($process->{'Name'}) eq $cmdname ) {
$message .= "$cmdname is running with PID = " . $process->{'P
+rocessID'} . "\n";
}
}
if ($message) {
print $message
return 1;
}
return 0;
}
sub help_and_exit {
print <<EOF;
$0
Syntax:
-r <remote computername>
-c <command> [Remember to use quotes if there are spaces!]
-h print this and exit.
EOF
exit(1);
}
__END__
-
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.