Batch file in Scheduled Task completes only when user is logged onto server

Hi there,

I have a scheduled task with these properties:

User account to run the task: A Service account that has logon as batch file privileges
Run whether user is logged on or not is enabled
Run with highest privileges is enabled
Configured for Windows Server 2012 R2
Trigger: Runs hourly
Actions values:
  • Action: Start a program
  • Program/script: Powershell.exe
  • Arguments: -command "Start-Process  C:\Build.bat -Verb runas"
  • Start in: C:\SDS_IT\

Now the problem I am having is that even though I have set the task to run whether user is logged on or not, it never completes unless the user is actually logged on.  If it I log out, the task starts and it gets to about 95% done and then it just gets stuck.  I've tested this theory several times and this is always the case.  I'm confused as to why it's not working.  The service account has local administrative permissions on this VM too.  I'm using PoweShell because I want the script to run a command prompt as administrator.

Any thoughts????
Thank you in advance!
-Christian
Christian PalaciosSenior IT Systems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gerwin Jansen, EE MVETopic Advisor Commented:
So it does start when you're not logged on?

Without knowing the contents of the batch file, it very much looks like one of the last commands in the batch is using some temporary storage or some location that is not available when the batch is run when you're not logged on.

Can you post the (redact if needed) batch file so we can try and figure out what is causing the issue? If  you can't what you could to is modify the batch script so that it logs what command is being executed by putting an extra 'echo' line before that command, redirecting that to a file.

Something like:

command1
command2

would become:

echo command1 >> log.txt
command1
echo command2 >> log.txt
command2
Steve KnightIT ConsultancyCommented:
Agreed with that, you could also amend the batch file removing echo off and add ( ) to log all commands and output to a file, e.g.

(@echo on
echo My batch file bits

) > C:\somedir\logfile.txt

Also unless I have mis-read or part of larger powershell script why are you using Powershell "Start-process" with runas to run the batch file rather than just having the scheduled task run batch file with cmd?

<Edit: sure that last sentence didn't show up before until I posted, OK you needed run-as-admin then>

Steve
Christian PalaciosSenior IT Systems AdministratorAuthor Commented:
Hi there,

Thanks for the suggestion Steve, I didn't think about using an output file to see what's happening.  It's working for the sections that I need to troubleshoot.  Does anyone have experience with using Git?  If not, that's OK, I'll create a new question.
Gerwin Jansen, EE MVETopic Advisor Commented:
>>  Does anyone have experience with using Git?
So it's a git command that is hanging / causing the issue?

Can you add a 'fake' command in the batch file:

git --version >> log.txt

Do you get an error? Then try adding setting the git environment variables in your script and add it to the PATH.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Git commands might need a -f (force) or -y (answer "Yes") switch to remove prompting.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.