0

I do see a strange behavior when running PowerShell scripts triggered by Windows Task Scheduler.

So that's my setup: Win2016 Task in Task Scheduler:

  • Action: Run powershell.exe with Arguments: "\Path\to\my\Script.ps1"
  • Run whether user is logged in or not
  • Run with highest privileges
  • Trigger: Run every Friday

The Script.ps1 actually starts a couple of other PowerShell scripts with Start-Process.

Now …

Behavior 1: When I start the task by myself by clicking "Run task" within the Task Scheduler, the script.ps1 runs just fine, starts all the sub-scripts that all run fine as well.

Behavior 2: I wait for the task to run by itself on Friday. The scripts.ps1 runs fine, starts all the sub-scripts and the task finishes fine. But the sub-scripts they just hang. I see them in the task manager, all with a couple of memory allocated (Like 14MB that does not change), but they do nothing. Its like they are started but do not run the first line of the script. Even more strange is, that some of the sub-scripts run just fine, like 2 out of 10, but the other 8 just stuck forever.

What is going on here?

marcuse
  • 21
  • 3
  • Where are the scripts stored (main and sub-scripts)? How do you call your sub-scripts? – stackprotector Jul 06 '20 at 06:13
  • Scripts are stored under C:\subdir\ The sub-scripts are called like this: `$process = Start-Process -NoNewWindows PowerShell.exe "C:\subdir\subscript.ps1"` – marcuse Jul 06 '20 at 11:14
  • Ok, so I guess `SYSTEM` will have access to those locations. Did you specify `SYSTEM` as the identity to run your scripts as? You problem will most probably be related to permission issues. Besides that, can you post a minimal example of a script that will hang? – stackprotector Jul 06 '20 at 11:46
  • The user to run the scripts is my user not `SYSTEM` – marcuse Jul 06 '20 at 14:10
  • Generally task scheduler will use cmd.exe syntax and since usuallyt the default opening for powershell scripts are in notepad, stuff is kinda weird. Try `PowerShell.exe -noprofile -Sta -executionpolicy bypass -File "FilePath"` for the action with the arguments in the add arguments section. – Nico Nekoru Jul 07 '20 at 19:13
  • Does [adding -Passthru help](https://stackoverflow.com/a/20790955/11609403)? – leeharvey1 Jul 09 '20 at 12:27

0 Answers0