Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Using WMI for create a remote Process on Win32

by Sniper (Scribe)
on Jun 19, 2001 at 14:11 UTC ( #89593=snippet: print w/ replies, xml ) Need Help??

Description: I use this Snippet for create remote Process on Win32
it is ok for simple process but you should see the WMI SDK
for complex process user rights dependants

David "Sniper" Rigaudiere

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;
}

Comment on Using WMI for create a remote Process on Win32
Download Code
Re: Using WMI for create a remote Process on Win32
by Umlaut (Novice) on Jun 29, 2001 at 21:11 UTC
    Very interesting. Do you know of a way to transfer a command to that remote machine and then execute it? That is, without pre-installing FTP or some other service.
    I know this is possible -- psexec (www.sysinternals.com) and HP's OpenBackup allow for remote installs.


    Thanks, Umlaut

      I copy the files via the admin share \\COMPUTERNAME\C$ then i use the WMI to run it...

      David "Sniper" Rigaudiere

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://89593]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-09-23 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (212 votes), past polls