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

I have a requirement to be able to run a command from the start menu as an arbitrary user, given the username and password. Any idea how I can accomplish this in Win32?

Win32::Process does not allow this (I've looked), and it appears I could just use the "RunAs" command, but the problem with that is you can't specify the password on the command line -- I intend to gather the password from a Tk dialog. So, if I did use RunAs, I'd need to drive it with something like Expect (but for Win32). Any ideas how to do this cleanly?

Any good module recommendations? I'd rather not shell out to runas if I can help it, since I'm not sure runas ships with all Windows versions, and I'm not sure how to feed the password to the RunAs.exe binary once it is running.

Thanks folks -- I appreciate your help!

  • Comment on Running Win32 binary as arbitrary user -- in Perl

Replies are listed 'Best First'.
Re: Running Win32 binary as arbitrary user -- in Perl
by gellyfish (Monsignor) on Apr 29, 2004 at 14:29 UTC

    You'll be wanting to look at Win32::AdminMisc - An example of using LogonAsUser and CreateProcessAsUSer can be be found here.


      A good discussion on how to do it and potential pitfalls in the implementation of a sudo clone on Win32 is available here. Although the examples are for Python, the system calls and pitfalls are all the same. If you can't get that link to work, try the Yahoo Cache of the page.

      - - arden.

      Thanks, I'm familiar with the C++ version of those API's from some earlier work. Good examples, too!
Re: Running Win32 binary as arbitrary user -- in Perl
by flyingmoose (Priest) on Apr 29, 2004 at 15:01 UTC
    There is a catch-22 here, of course. You need to have 'act as part of the operating system' rights for the current user to be able to call LogonUser ... hence, you need to be an Administrator (or a system service) to impersonate the Administrator :)

    All I want to do is start/stop a service while not running as Administrator, so I guess I will need to use RunAs after all -- anybody know the Windows equivalent of ???

      Would Win32::Service help? There's a suite of service management functions including: StopService.

      You might be able to get that to play nicely with Win32::AdminMisc CreateProcessAsUser.

      If you have much Win32 stuff to play with Dave Roth's book "Win32 Perl Programming: The Standard Extensions, Second Edition" is well worth checking out.

        Yes, it will help the code (thanks!) -- just won't get me around the evil act-as-part-of-OS-permission problem!

        BTW, I didn't know David Lee Roth was into Perl. Awesome!

Re: Running Win32 binary as arbitrary user -- in Perl
by flyingmoose (Priest) on Apr 29, 2004 at 18:37 UTC
    OT, but all of this Windows programming work lately has made me realize how much better it is to run CPAN (and Perl) on Linux. Need Term::ReadKey for this, need Module::Build, need stty, need a different Term::ReadLine plugin, need make, need a compiler, need this environment set up... Ah, Penguins, how do I love thee :) On Linux, stuff just works (TM).

    Anyhow, I have a solution now -- thanks everyone!