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->LastError(); $Process = $WMI->Get( "Win32_Process" ) or die "Unable to get a Win32_Process :" . Win32::OLE->LastError(); $vPid = Variant( VT_I4 | VT_BYREF, 0 ); # $vPid (out) PID of 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; }