2

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.

1 Answers1

3

stop-computer needs the -force flag to work I guess when running as a process. Switching to stop-computer -force fixed the problem.