简体   繁体   English

Powershell - 从 Powershell 远程转发事件 Session - 行为不一致

[英]Powershell - Event Forwarding from Powershell Remoting Session - Inconsistent Behaviour

I am noticing a slightly inconsistent behaviour between PowerShell ISE and PowerShell Console.我注意到 PowerShell ISE 和 PowerShell 控制台之间的行为略有不一致。 I am trying to run the below script, which attempts to display the forwarded event data from a remote PowerShell session on the host computer.我正在尝试运行以下脚本,该脚本尝试在主机上显示来自远程 PowerShell session 的转发事件数据。 It works as expected when run from ISE but when I run it from the PowerShell Console, no forwarded messages are displayed.从 ISE 运行时它按预期工作,但当我从 PowerShell 控制台运行它时,不显示转发消息。 But when I press the 'Tab' key then suddenly all the queued messages appear at once.但是当我按下“Tab”键时,突然所有排队的消息都会立即出现。

I have noticed this behaviour on Windows Server 2008 R2 as well as later OSes.我在 Windows Server 2008 R2 以及更高版本的操作系统上注意到了这种行为。 I am using PowerShell 5.1.我正在使用 PowerShell 5.1。

Any ideas?有任何想法吗? Thanks.谢谢。

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
    }
}

The ISE will make things available (via autoloading) that you must explicitly initialize/call when running in the ISE. ISE 将使您在 ISE 中运行时必须明确初始化/调用的东西可用(通过自动加载)。 IE, Forms namespaces, Security settings (TLS), etc. So, if you are saying that you won't see the output of the $RemoteJob info, you have to tell the consolehost about that. IE、Forms 命名空间、安全设置 (TLS) 等。所以,如果你说你不会看到 $RemoteJob 信息的 output,你必须告诉控制台主机。

So, using the Output cmdlets, or variable squeezing (which assigns the results to a variable while simultaneously sending the results to the screen.)因此,使用 Output cmdlet 或变量压缩(将结果分配给变量,同时将结果发送到屏幕。)

So, try these:所以,试试这些:

# 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
    }
})

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

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