当用户中断powershell脚本时,有没有办法捕捉到。
我需要退出 PSSession。
我已经使用 try finally 进行了测试,但是脚本中断时 finally 没有执行Ctrl+c
try
{
$s = New-PSSession -ComputerName MYCOMPUTER -Credential (Get-Credential -Credential admin )
Invoke-Command -ScriptBlock { Get-EventLog -LogName Application } -Session $s
}
catch [Exception]
{
}
finally
{
echo "Ending the session"
Remove-PSSession $s
}