I have a script on linux that connects to a remote. When exiting, I have to exit twice (once from the remote, and once from the local). I would like to connect these two, so that I have to exit only once.
Here is what I hoped would work:
pwsh -NoExit -Command '
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action {
Write-Host "Received Exiting"
}
$creds = Get-Credential -UserName sysadm;
$pss = New-PSSession -ComputerName remote-pc -Authentication Negotiate -Credential $creds;
Invoke-Command -Session $pss {
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Forward;
}
Enter-PSSession $pss
'
Based on the Register-EngineEvent docs I expected to see the message - and manually creating the event worked:
...
[remote-pc] PS C:\Users\sysadm\Documents> $null = New-Event PowerShell.Exiting
Received Exiting
[remote-pc] PS C:\Users\sysadm\Documents> exit
# Expecting a message here...
PS /home/local-user > $null = New-Event PowerShell.Exiting
Received Exiting
PS /home/local-user > exit
Received Exiting
I hope I was able to describe my problem.
Why isn't exiting the session sending the PowerShell.Exiting
event?
If not via PowerShell.Exiting
, is it possible to achive the "double-exit" in an other way?
I don't think you need event handling in your case:
Just place an exit
statement after Enter-PSSession $pss
, and your outer PowerShell session will exit too:
pwsh -NoExit -Command '
$creds = Get-Credential -UserName sysadm;
$pss = New-PSSession -ComputerName remote-pc -Authentication Negotiate -Credential $creds;
Enter-PSSession $pss
exit
'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.