我正在尝试将powershell脚本与记事本同时退出。该脚本应在后台执行文件备份,因此它与while(1)
一起运行。我订阅了exited
记事本流程事件,但exit
命令似乎无法在Action
块中工作。当我关闭记事本时没有任何反应。
$gameRunning = Get-Process notepad -ErrorAction SilentlyContinue
if ($gameRunning) {
Register-ObjectEvent -InputObject ($gameRunning) -EventName exited -Action {exit} -SourceIdentifier NotepadRunning
}
else {
Write-Host "Game not running."
}
while(1)
{}
知道如何让它从Action范围退出吗?
答案 0 :(得分:1)
Register-ObjectEvent $gameRunning -EventName Exited -Action {[Environment]::Exit(0)} -SourceIdentifier NotepadExited