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

I use perl for this, but I am not sure if the problem is perl realted. Here goes anyway...

We have a exe that needs to be run every hour (scheduled task). But before it can be run a few files need to be created from databases and text files and so on...which we use Perl to do.

So our schedule is a perl script that does the things it needs to do in perl, and then uses system() to run the exe.

However, our exe is failing to run. We don't exactly know why, but for some reason it will only run when there is a user logged in, and that user id is the same one that the job is running under.

The exe is an external program that cannot be changed.

So I figure that maybe to solve this I need to fake a login of the user_id running the job, so that when there is no one logged into the server (most of the time) then the job will still work.

Does anyone know if there is a way in Perl for windows to simulate a login?

For information, the user_id running the job has been tested with every possible NT priv, just incase there was a way to do it that way...still not working properly.

Any suggestions would be appreciated.

Thanks

Replies are listed 'Best First'.
Re: Win NT user impersonation in Perl
by caedes (Pilgrim) on Jul 05, 2002 at 14:26 UTC
    It sounds like you would have to run this script as an NT service in order to get it to run when no one is logged in.

    Update: I just tested this in Windows 2000, you can set a scheduled task to run any time the computer is on by first logging in as administrator and then making the task from there. It will ask for a user and password to run the script as. This will ensure that the script runs even if no one is logged into the machine.

    -caedes

Re: Win NT user impersonation in Perl
by fglock (Vicar) on Jul 05, 2002 at 17:08 UTC
    The program "SRVANY" from the "NT Resource Kit" allows a program to run as a service.
    From: Paul Joslin (Paul.Joslin@sdrc.com) Subject: Re: Perl as an NT Service Newsgroups: comp.os.ms-windows.nt.admin.misc, comp.os.ms-windows.nt Date: 1999/07/14 From the FAQ: > How do I run a Perl script as a Windows NT Service? > > You can run your Perl scripts as Windows NT Services via a program c +alled srvany.exe, which comes with the > Windows NT Resource Kit. Once srvany.exe is installed, read the srva +ny.wri file which should be with it. This > document will explain how to set up registry entries for your new se +rvice. > > After you are set up, to run your script as a service do: > > x:>srvany perl script.pl >
Re: Win NT user impersonation in Perl
by thunders (Priest) on Jul 05, 2002 at 18:13 UTC
    If you need to create and run a process as another user there is indeed a module that does this called Win32::AdminMisc it has the methods LoginAsUser() and CreateProcessAsUser() which should do what you need.

    if the link above is down, it was when i wrote this, try this google cache

Re: Win NT user impersonation in Perl
by flounder99 (Friar) on Jul 05, 2002 at 14:39 UTC
    This may be something simple. Here are some things to check.

    1) Be sure to include the full pathname to the exe file. You may be able to run the exe as a user because the user has the exe on his/her PATH and the PATH is not set up when run with no one logged in.

    2) Is the external program on a mapped drive? The mapping only happens when a user is logged in. You may not be able to get to the exe because the mapping does not exist.

    --

    flounder

Re: Win NT user impersonation in Perl
by RMGir (Prior) on Jul 05, 2002 at 14:40 UTC
    You're right, I don't think this is a perl issue. But I have run into similar problems in the past.

    I believe that by default, the network shares aren't available when there's no user logged in.

    I think that can be corrected by changing the "Schedule" service to run as a user. I don't remember if by itself, that's enough to mount the drives, but you could probably also wrap the exe call in a batch file that mounts the drives if they're not already there, but you have to make sure the user Schedule runs as is allowed to access the network.

    Of course, you could have some other, completely different, problem. Aren't computers fun? :)

    (I'm not even gonna blame NT for this one, since there are similar traps waiting in the Unix world...)
    --
    Mike

Re: Win NT user impersonation in Perl
by Anonymous Monk on Jul 05, 2002 at 14:53 UTC
    Thanks people.

    Actually I have tried running as a service (including one interacting with the desktop), I have tried running it under different user ids, the paths are full paths, there are no mapped/network drives in it.

    Most likely it is something hardcoded into the exe itself which requires a user to be logged into NT, so I was going to attempt to fake a login and then run the exe.

    Could be I may just have to re-write the exe myself...that would suck.