How to invoke a process as another user on Windows?

I have a Perl script and I need to run it as a different user in a local box. This is a test machine, so I wouldn't use any real security if it worked.

I have tried so far

$cmd = 'runas /user:tester01 "perl delegated.pl"';
system($cmd) == 0
    or die "could not spawn process as tester01: $!";

      

but that doesn't work: it asks for the password interactively. (Pipeline echo password

in runas

too failed).

How do I achieve this from a Perl script? Or do I need to do something else before this part system "runas..."

?

I know I can use PsExec, but I would prefer a native Windows solution. Currently the boxes it needs to work on are Windows 7 and Windows XP, but other Windows OSs could be added later.

+3


source to share


3 answers


If you don't want to play with WinAPI to send a password to a spawned wrapper (for example, the Win32 :: GuiTest SendKeys method might be useful), it's better to use PsExec. ) This thread is a little outdated, but I think it describes Microsoft's policy pretty well.



+3


source


This is how your code will look like:

use strict;
use warnings;
use Win32::GuiTest qw[ SendKeys ];

system 1, q[runas /user:machinename\username "perl scriptname.pl"];

SendKeys( 'password~');

      



For details on SendKeys () see Win32 :: GuiTest .

+2


source


I think what you want

system($cmd) == 0
    or die "could not spawn process as tester01: $!";

      

Your code $cmd==0

will evaluate to a value of 1 since it $cmd

converts to the number 0 and 0==0

then it system

will pass 1 and try to run it as a command.

You should use warnings;

report the problem when $cmd

it was implicitly converted to a number.

0


source







All Articles