I am trying to make a powershell script to exit simultaneously with notepad. The script should do file backups in the background, so it runs with while(1). I subscribe to exited event of notepad process, but exit command doesn't seem to work in Action block. Nothing happens when I close notepad.
$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)
{}
Any idea how to make it exit from Action scope?
$gameRunning.HasExitedin safe to interrupt points. Otherwise you are risking to interrupt your background backup in undetermined state.