PDA

View Full Version : Task scheduler problem



Bob Worsley
9-Dec-2021, 08:19 AM
I wrote a program that extracts data from Quickbooks that is called by a nightly Windows scheduled task. This program and Quickbooks are located on a Windows 10 Pro box that I can remote into. The program opens the Quickbooks company file and writes the data to some CSV files, all of which works fine when I'm remoted in. The problem I've run into is that the Quickbooks company file is located on a mapped drive and when the job runs automatically in the middle of the night the mapped drive isn't mapped and the job fails.

I tried using UNC for the company file but Quickbooks doesn't seem to understand UNC when I try to open the file.

The second thing I've tried is to create a batch file which is called from the scheduler that first maps the drive and then calls my program. Supposedly the ID I'm logging on with has admin rights (I checked, it does) so a simple net use statement should work. I tested this last night and it seemed to work but only that once. When it ran in the middle of the night, no joy, can't find the "R" drive.

If this was a real server I think a script to do this could be created but it isn't. Does anyone have any suggestions?

Garret Mott
9-Dec-2021, 09:12 AM
While I'd think the Net Use would work, have you tried logging into the machine as the user running the task & creating the mapped drive? Also check permissions on it?

Most times for a scheduled task I run it as a Services account - which has full permissions. Might be overkill...

Bob Worsley
9-Dec-2021, 09:28 AM
have you tried logging into the machine as the user running the task & creating the mapped drive? Also check permissions on it?Yes, I did. I used a different drive letter too, just so I would be sure it was my drive and no joy. In reading up on this it's apparently very difficult if not impossible to map a drive from a scheduled task. From Googling I keep seeing this statement: "Mapped drives are only available to an interactive session. You can't map a drive while running as a service, you need to use UNC paths." Maybe I can move the program to the location of the mapped drive, it's a different server.

wila
9-Dec-2021, 01:49 PM
"Mapped drives are only available to an interactive session. You can't map a drive while running as a service, you need to use UNC paths."

That's what I was wondering. You're running in session 0 when not logged in and running that task.
Not sure if "mapping drives" is impossible in session 0 though.

Have you tried to spawn a CMD process and then run everything from within that via a script?
--
Wil

Bob Worsley
9-Dec-2021, 01:56 PM
Have you tried to spawn a CMD process and then run everything from within that via a script?Yes I did. I call a batch file from the task scheduler and in it I first run a net use statement to map the drive and then call my application. Didn't work. Must be that the above statement about not being able to map drives in a service applies...

wila
9-Dec-2021, 04:36 PM
Yes I did. I call a batch file from the task scheduler and in it I first run a net use statement to map the drive and then call my application. Didn't work. Must be that the above statement about not being able to map drives in a service applies...

Not what I meant..
I wasn't asking if you called a batch file, but if you start a command interpreter and from WITHIN that command interpreter run a batch file.
The CMD process HAS to be the parent process.
Without that you don't have the environment you might need for your batch file.

Open a command prompt and type:


CMD /?


Then read... the option I am talking about is /C from the looks of it (or /K if you want).
From that.. you THEN call the script...
--
Wil

Bob Worsley
9-Dec-2021, 04:38 PM
Hmmm... not familiar with doing that but will see what I can figure out