1

I have had a ton of problems trying to set up some automated tasks that execute a batch file which in turn run python scripts.

All of these tasks run fine when they are run under my username and I am logged on, but when I set them to be run under a service account when I am not logged on 1) I can't get them to run and 2) I don't really know how to troubleshoot.

Some of the testing I did involved

  • Just getting a batch file to execute and write a line of text to a file indicating domain\user and time.
  • Doing the same thing except executing a python script that wrote a line of text to a file. This showed me the service account had access to python, and was able to write to the file.
  • The same thing again, but this time executing a python script located on a shared directory and writing to a file in that same location, showing the task could execute the script and access the same location.
  • I looked at this question and am confident I have everything there set up properly, such as local security policy stuff, permission to run batch job, etc.
  • I made sure my scripts used UNC paths instead of drive mappings

From there it got trickier. For starters, and maybe I'm doing something wrong here, the history tab in TaskScheduler does not seem to be helpful to me. There is nothing along the lines of "this task tried to read this file but didn't have permission". Also, when the service account runs the task, there is no console window so I can't see any output. I would usually get an opcode of 2 (file not found?) or 1 (incorrect function called) but the history tabs have no details about what wasn't found.

In one case I was able to run a python script and write to a file ok, but when I added code to connect to a database, it just died. In another case when I removed an import in the script it was able to run, but not after putting it back in.

The settings in Task Scheduler (and this applies to numerous tests and scripts I've tried) is to set the domain\user of the service account under "user the following account" and clicking the checkbox for "run whether user is logged on or not." I also checked "run with highest privileges" and made sure I had the location of the batch file listed under Action.

here is the text of the batch file:

echo testing batch file :  %time% %USERDOMAIN%\%USERNAME% >> statenet-test.log


IF %ERRORLEVEL% NEQ 0 ECHO %date% %USERDOMAIN%\%USERNAME% >> statenet-test.log mycommand wasn't found

python --version  >> statenet-test.log mycommand wasn't found

if exist %script_dir%\statenet_connect_test.py (
  echo script file exists %time% %USERDOMAIN%\%USERNAME% >> statenet-test.log
  python %script_dir%\statenet_connect_test.py
    ) else (
  echo can't find script file %time% %USERDOMAIN%\%USERNAME% >>     statenet-test.log
)

here is a screenshot of the history tab:

task history screenshot 2

and the actions tab action tab screenshot 3

and another issue...the task is listed as still running long after it's actually died. I can't see anything in the batch file (there is no 'pause' at the end) that would keep it going. Could that be related to the root cause?

It seems to me like this really should work without and issue, and I'd appreciate any troubleshooting advice.

thanks!

Update

this issue was solved when the service account was given login permissions. When I logged in as the service account and tried to run the batch files, the pip packages were installed for my main user and not the service account.

  • please show us how your tasks actions look. – Manu Aug 23 '22 at 11:50
  • just updated and added some screenshots – jasonwardenburg Aug 23 '22 at 13:38
  • Aslong as the Batch-File is a Top-Secret, it will be nearly impossible to help you. moreover, usually you run cmd with the statement as batch and thereforce run in a specific folder – djdomi Aug 23 '22 at 13:52
  • sorry for the oversight, just added a copy of the batch file. I added an if statement to verify it could find the dir and find the script. The log file referred to in the batch file gets the output I expect, it's the file generated by the script that doesn't work. – jasonwardenburg Aug 23 '22 at 14:04

0 Answers0