简体   繁体   English

在 Linux 上远程处理时如何转发 Powershell.Exiting 事件

[英]How to forward Powershell.Exiting event when remoting on Linux

I have a script on linux that connects to a remote.我在 linux 上有一个连接到远程的脚本。 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:根据Register-EngineEvent 文档,我希望看到该消息 - 手动创建事件有效:

...
[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?为什么不退出会话发送PowerShell.Exiting事件?

  • If not via PowerShell.Exiting , is it possible to achive the "double-exit" in an other way?如果不是通过PowerShell.Exiting ,是否可以通过其他方式实现“双退出”?

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:只需在Enter-PSSession $pss之后放置一个exit语句,您的外部 PowerShell 会话也会退出:

pwsh -NoExit -Command '
  $creds = Get-Credential -UserName sysadm;
  $pss = New-PSSession -ComputerName remote-pc -Authentication Negotiate -Credential $creds;
  Enter-PSSession $pss
  exit
'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM