I have a task running on a Hyper-V server setup as such:
schtasks /CREATE /TN AutoShutdown /RU "SYSTEM" /TR "Powershell \"C:\AutoShutdown.ps1\"" /SC ONSTART
It runs properly (as far as I can tell) however the script has a line to shutdown the computer and it never does.
start-sleep 120
while (1) {$vm = get-vm; if ($vm.state -ne "Running") {stop-computer} else {"Running..."; start-sleep 10}}
If I run the task in user space:
schtasks /CREATE /TN AutoShutdown /TR "Powershell \"C:\AutoShutdown.ps1\"" /SC ONLOGIN
It runs perfectly.