Take the 2-minute tour ×

I have a Perl script and I need to run it as another user on the local box. It's a testing machine, so no real security—I would use hammer if it worked.

What I've tried so far is

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

but that does not work: it asks for password interactively. (Piping echo password into the runas failed as well.)

How should I achieve it from within Perl script? Or should I do something else before that system "runas..." part?

I know I can use PsExec, but I'd prefer Windows native solution. Currently the boxes it should work on are Windows 7 and Windows XP, but other Windows OS might be added later.

share|improve this question
You can try Expect perl module that allows a script to work with interactive programs. –  DA14 Mar 10 '12 at 7:09
add comment

3 Answers

up vote 3 down vote accepted

If you don't want to play with WinAPI to send a password into the spawned shell (for example, Win32::GuiTest SendKeys method might be useful), you'd better use PsExec. ) This thread is a bit old, but it still describes Microsoft policy pretty well, I guess.

share|improve this answer
I finally chose PsExec to do my job. I could make runas + Win32::GuiTest run, but unfortunately not very reliably, especially if there was larger amount of processes to be spawned quickly. –  Alois Mahdal Mar 13 '12 at 13:37
(Again the old rule for scripting on Windows proved: most "native" way on Windows is avoiding the native ways) –  Alois Mahdal Mar 13 '12 at 13:39
add comment

I think you want

system($cmd) == 0

In your code $cmd==0 will evaluate to 1 since $cmd will convert to the number 0 and 0==0, then system will be passed 1 and try to run it as a command.

You should use warnings; it would have reported an issue when $cmd was implicitly converted to a number.

share|improve this answer
Thanks, that was just a typo in my example. Of course I do use warnings; :) –  Alois Mahdal Mar 9 '12 at 22:35
add comment

Here's how your code would go:

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

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

SendKeys( 'password~');

See Win32::GuiTest documentation for more details on SendKeys().

share|improve this answer
Alois Mahdal- It works.But needs a enter in execution. –  DA14 Mar 12 '12 at 14:12
However, this does not really fit for scripting, i.e. (a) you need to avoid interaction, and (b) things seem to get very messy as long as this snippet is put into a loop or so. Probably more playing with Win32::GuiTest could help, though... –  Alois Mahdal Mar 12 '12 at 16:29
@AloisMahdal - Welcome and Thanks.I will check on how to avoid enter. –  DA14 Mar 12 '12 at 16:32
After looking at first few lines of Win32::GuiTest I could finally make it work. The secret s the tilde (~) that does press the [Enter] for you. –  Alois Mahdal Mar 12 '12 at 16:54
Still, though, sometimes some of iterations in the loop still fail (refusing the password), sometimes all pass. Reasons yet unknown...est –  Alois Mahdal Mar 12 '12 at 17:03
show 1 more comment

Your Answer


