Powershell-从Powershell远程处理会话转发事件-行为不一致

Powershell-从Powershell远程处理会话转发事件-行为不一致,powershell,Powershell,我注意到PowerShell ISE和PowerShell控制台之间的行为稍有不一致。我正在尝试运行以下脚本,该脚本尝试显示主机上远程PowerShell会话转发的事件数据。从ISE运行时,它会按预期工作,但从PowerShell控制台运行时,不会显示转发的消息。但当我按下“Tab”键时,突然所有排队的消息同时出现 我在WindowsServer2008R2以及更高版本的操作系统上注意到了这种行为。我正在使用PowerShell 5.1 有什么想法吗?谢谢 Register-EngineEven

我注意到PowerShell ISE和PowerShell控制台之间的行为稍有不一致。我正在尝试运行以下脚本,该脚本尝试显示主机上远程PowerShell会话转发的事件数据。从ISE运行时,它会按预期工作,但从PowerShell控制台运行时,不会显示转发的消息。但当我按下“Tab”键时,突然所有排队的消息同时出现

我在WindowsServer2008R2以及更高版本的操作系统上注意到了这种行为。我正在使用PowerShell 5.1

有什么想法吗?谢谢

Register-EngineEvent -SourceIdentifier RemoteEventOccured -MessageData 'RemoteEventOccured' -Action {
    Write-Host $event.MessageData
} | Out-Null

$RemoteJob = Invoke-Command -ComputerName localhost -AsJob -JobName MyRemoteJob -ScriptBlock {

    Register-EngineEvent -SourceIdentifier RemoteEventOccured  -Forward | Out-Null

    while($true){
        New-Event -SourceIdentifier 'RemoteEventOccured' -MessageData "$(Get-Date): Remote data received..." | Out-Null
        Start-Sleep -Seconds 5
    }
}

ISE将提供在ISE中运行时必须显式初始化/调用的内容(通过自动加载)。例如,表单名称空间、安全设置(TLS)等。因此,如果您说您将看不到$RemoteJob信息的输出,您必须告诉consolehost

因此,使用Output cmdlet或变量压缩(将结果分配给变量,同时将结果发送到屏幕)

那么,试试这些:

# Using Out-Host
$RemoteJob = Invoke-Command -ComputerName localhost -AsJob -JobName MyRemoteJob -ScriptBlock {

    Register-EngineEvent -SourceIdentifier RemoteEventOccured  -Forward | 
    Out-Null

    while($true)
    {
        New-Event -SourceIdentifier 'RemoteEventOccured' -MessageData "$(Get-Date): Remote data received..." | 
        Out-Null
        Start-Sleep -Seconds 5
    }
} | Out-Host



# Write
Write-Output $RemoteJob



# Variable squeezing
($RemoteJob = Invoke-Command -ComputerName localhost -AsJob -JobName MyRemoteJob -ScriptBlock {

    Register-EngineEvent -SourceIdentifier RemoteEventOccured  -Forward | 
    Out-Null

    while($true)
    {
        New-Event -SourceIdentifier 'RemoteEventOccured' -MessageData "$(Get-Date): Remote data received..." | 
        Out-Null
        Start-Sleep -Seconds 5
    }
})

哇!头脑=崩溃。谢谢尽管我没有发现直观的解决方案,但变量压缩方法感觉相当舒服。但是,我的原始脚本包含了足够的代码,可以让我清楚地了解将要发生什么,但事实上,它的工作方式并没有让我感觉到“变通方法”在某种程度上违背了PowerShell的原始设计原则。我可能又错了。再次感谢,不用担心。事情可能是阴云密布的,直到有一段时间我们不得不把它们弄得一团糟。这只是3个选项。使用cmdlet直接写入输出,使用显式Wirte-*命令和该变量压缩,将输出分配给变量,同时发送到屏幕。这是我常用的穷人调试方法,这样我就可以看到每个阶段的所有结果,以确保在没有额外编码的情况下得到预期的结果。