Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

su on winnt

by mce (Curate)
on Nov 09, 2001 at 17:27 UTC ( #124351=perlquestion: print w/replies, xml ) Need Help??
mce has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow Monks,

I am working on a scheduling project, and we want to be able to run scripts on WinNT. The daemon we are running, is running under the system account, but the scripts should be ran as another user. On unix, this is very simple, and I just use the unix su command.
But on NT this is not so simple. Now my question is, has someone already written a routine that runs a process as nother user. I searched CPAN and this site, but found no examples on how to do this. There are modules like > Win32::Process and Win32::AuthenticateUser, but I cannot make the link between these two modules.
An NT port of su exists, but does not return the errorcode of the script.

Mark Ceulemans
Senior Consultant
IT Masters, Belguim

Replies are listed 'Best First'.
Re: su on winnt
by c-era (Curate) on Nov 09, 2001 at 18:54 UTC
    Use the Win32::AdminMisc module available from

    Here's how to use ppm

    PPM> set repository roth PPM> install Win32-AdminMisc
    As long as the program is runing as the system user, you can use this piece of code to change the user.
    I hope this helps.
Re: su on winnt
by Asim (Hermit) on Nov 09, 2001 at 18:58 UTC

    Take a look at Dave Roth's Win32::AdminMisc program. From the FAQ:

    use Win32::AdminMisc; use Win32; if (Win32::AdminMisc::LogonAsUser($Domain, $User, $Password, LOGON32_L +OGON_INTERACTIVE)){ print "Successfully logged on.\n"; print "\nLaunching $Program...\n"; $Result = Win32::AdminMisc::CreateProcessAsUser( $Process, "Flags", CREATE_NEW_CONSOLE, "XSize", 640, "YSize", 400, "X", 200, "Y", 175, "XBuffer", 80, "YBuffer", 175, "Title", "Title: $User" . "'s $Pr +ocess program", "Fill", BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_GREEN, ); if ($Result){ print "Successful! The new PID is $Result.\n"; }else{ print "Failed.\n\tError: " . Win32::FormatMessage(Win32::Admin +Misc::GetError()) . "\n"; } }else{ print "Failed to logon.\n\tError: " . Win32::FormatMessage(Win32:: +AdminMisc::GetError()) "\n"; } Returns: undef if unsuccessful process id (PID) if successful

    As a side note, if you happen to have any Win2K servers, the runas command <emphasis>finally</emphasis> gives you a command-line su in Win32.

    ----Asim, known to some as Woodrow.

      Wow, Super cool this module.
      Why isn't this module available from CPAN?
      Dr. Mark Ceulemans

      Senior Consultant

      IT Masters, Belgium

Re: su on winnt
by CubicSpline (Friar) on Nov 09, 2001 at 18:32 UTC
    This is what I took out of your question, please correct me if I'm wrong.

    1. You have a daemon process running on an NT system.
    2. You have a script that needs to run under a user account, not the system account.

    If that is the case, I would look into seeing about Turning a Perl Program into an NT Service. By doing this, you can set your perl daemon to be started up automatically as an NT service and it will be running in the background for whoever logs into that machine. Then your client script can be run under the user account.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://124351]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-06-20 00:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.