I have a Windows Server 2012 R2 (PowerShell 4).
There is a ps1 file with a PowerShell script, which contains among other things the command restart-computer.
I also created a cmd file which should be started via Task Scheduler.
- Everything works as expected when I start the cmd file by double-clicking it.
- Nearly everything works as expected except the reboot when the cmd is started via Windows Task Scheduler.
So I created a ps1 file which only executes restart-computer and a proper cmd file for testing purposes.
I also tried Restart-Computer -AsJob
powershell.exe -ExecutionPolicy Bypass -File "C:\restart-computer123.ps1"I also created the scheduled task configured for "Windows Server 2012 R2", with administrative credentials, highest privileges. and saved the password.
There is no error message in the event log or scheduler, but the server isn't rebooting.
What am I missing or doing wrong?
61 Answer
Writing the $error variable to a file brings light to the problem!
$error | Out-File C:\error.txtThere was an error, that there are other users logged on, and therefor it can't restart. With the "-Force" option it works.
Thank you SimonS to get me on the right track!