lisa_lionheart has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I've been using perl for test scripts for several years but have never run into a problem like this before. I'm hoping one of you monks can tell me if it's even possible before I spent a whole lot of time on it.

I'm trying to test a product by using the windows command "runas" to start the product in different user contexts. It's like a windows su. However the folks at MS have decided to make my life difficult. You can start a program as so...

c:\runas /user:winuser "program args"

and it will run program as if you are logged in as winuser. What they've done to make it difficult is that blank passwords may not be used with runas, and the program will not accept a password argument. Instead, as soon as you run this, you get a password prompt...

Enter the password for winuser:

So I'm kind of stumped by this. I've always just used system() and passed it an array containing the name of a program and a list of args. That's fine for the initial invocation of runas, but since it's a missile, there isn't any way to pass something else after the fact.

Interestingly MS did this deliberately to prevent people from doing just what I'm trying to do, except that I'm just trying to do it to test something...

It's a long shot but I was wondering if any of you smart folks could think of a way that a perl script could start runas and then provide a password after the prompt?

There is a windows function called CreateProcessWithLogonW() which looks like it could serve as the basis for a free standing executable that I could call from the scripts which in turn would spawn the application under test in different user contexts. I fear that might be a bit over my head though.

Replies are listed 'Best First'.
Re: interactive command session possible?
by BrowserUk (Patriarch) on Sep 14, 2011 at 08:42 UTC

    This cannot be done by piping characters to runas.exe stdin, because it doesn't read them from stdin, but rather fetches them directly from the process input queue.

    It can however be done by writing directly to the applications input queue, using Win32::GuiTest.

    #! perl -slw use strict; use Win32::GuiTest qw[ SendKeys ]; system 1, q[ runas /user:UserName "c:\path\\to\\the\\Command /switches + Args" ]; sleep 1; SendKeys( 'tHePa55W0rD~', 50 );

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      BrowserUk,

      Awesome suggestion. I'm going to try this first chance, but from a glance it looks pretty sound. So long as it accepts the keys, all it should take is some tweaking to the sleep value. Thanks!!!
        So long as it accepts the keys,

        SendKeys() can generate every possible character including Unicode, along with all of the non-characters keys such as the "Windows" and "Context menu" keys.

        all it should take is some tweaking to the sleep value.

        The sleep is there just to give the program time to start and display its window before the keystrokes are sent. The timing might be optimised by using WaitWindow() instead.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: interactive command session possible?
by DrHyde (Prior) on Sep 14, 2011 at 09:22 UTC
    On Unix the canonical hammer for this particular nail is Expect. No idea if it works on Windows, but even if it doesn't, perhaps just knowing its name will help you formulate a suitable googlism.
Re: interactive command session possible?
by thewebsi (Scribe) on Sep 14, 2011 at 04:51 UTC

    Yeah, as Marshall suggested, I would try piping the password first:

    system ( 'echo "password" | runas /user:winuser "command args"' );

    The open function is another way to do the same thing:

    open ( OUT, '| runas /user:winuser "command args"' ); print OUT "password\n"; close ( OUT );

    su is wise to this trick on my Linux box, so it doesn't work, but maybe Windows can be fooled.

Re: interactive command session possible?
by Marshall (Canon) on Sep 14, 2011 at 02:17 UTC
    So what happens if you make a .bat file:
    echo "thePassword" | start c:\runas /user:winuser "program args"

    If all else fails, it is possible to start a Perl program which then starts the desired program and acts like a person on the command line. In other words, if you are able to make "X" happen from the command line, Perl can too!

    I am no expert on Windows sys admin, but I think that there is some automatic reply thing that can go into a batch file.