use strict; use Win32::OLE::Variant; my ($opts_ret, $svr, $help_var, $usr, $pwd, $exe); use Getopt::Long qw(:config no_ignore_case); $opts_ret = Getopt::Long::GetOptions( 'server=s' => \$svr, 'username=s' => \$usr, 'password=s' => \$pwd, 'command=s' => \$exe, 'help!' => \$help_var); if (($help_var) | ($ARGV[0] =~ /\?/)){ &help; exit 0; } elsif(!$exe){ print "Syntax Error: command is required\n\n"; &help; exit 0; } my $Con = connectServer( $svr, $usr, $pwd ); print "STARTING EXE\n"; my $pid = startProcess( $Con, $exe, "C:\\temp"); print "PID IS $pid\n"; sub connectServer { my ( $svr, $usr, $pwd ) = @_; $svr ||= '.'; # just in cass I want to use args and run it on localhost my $locator = Win32::OLE->new("WbemScripting.SWbemLocator") or die "Can't access WMI on local machine.", Win32::OLE->LastError; my $Conn = $locator->ConnectServer($svr, "root/cimv2", $usr, $pwd) or die "Can't access WMI on remote machine $svr: ", Win32::OLE->LastError; return $Conn; } sub startProcess { my ( $Conn, $exe, $startdir ) = @_; my $startClass = $Conn->Get("Win32_Process") or die Win32::OLE->LastError; my $startConfig = $startClass->SpawnInstance_ ; my $pid = Variant(VT_I4|VT_BYREF, 0); my $retval = $startClass->Create( $exe, $startdir, undef, $pid ); if ( 0 == $retval ) { return $pid; } else { die "Could not start $exe errcode: " . decode_error($retval); } } sub decode_error { my %h = ( 1 => "Not Supported", 2 => "Access Denied", 8 => "Unknown Failure", 9 => "Path Not Found", ); return $h{$_[0]} ? $h{$_[0]} : "Can't resolve error code $_[0]"; } sub help{ print " Usage: rmtstart [-s ] [ -u \username ] [ -p ] -c OPTIONS: -s Defines server on which to run command. This is not a required argument, if not supplied the command will be run on the local computer. -u Defines user name or domain\username to run the command as. If not supplied currently logged on user on local computer will be used. -p Defines password of user running command. -c Defines Command to be run. Arguments may be passed and spaces may exist in pathname but in both of these cases you must enclose the command in quotes. "; }