我有一个任务在Hyper-V服务器上运行,如下所示:
schtasks /CREATE /TN AutoShutdown /RU "SYSTEM" /TR "Powershell \"C:\AutoShutdown.ps1\"" /SC ONSTART
它运行正常(据我所知),但脚本有一行closures计算机,它永远不会。
start-sleep 120 while (1) {$vm = get-vm; if ($vm.state -ne "Running") {stop-computer} else {"Running..."; start-sleep 10}}
如果我在用户空间运行任务:
schtasks /CREATE /TN AutoShutdown /TR "Powershell \"C:\AutoShutdown.ps1\"" /SC ONLOGIN
它运行完美。
停止计算机需要-force标志工作,我想当作为一个进程运行。 切换到stop-computer -force解决了这个问题。