1

I am noticing a slightly inconsistent behaviour between PowerShell ISE and PowerShell Console. 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. It works as expected when run from ISE but when I run it from the PowerShell Console, no forwarded messages are displayed. But when I press the 'Tab' key then suddenly all the queued messages appear at once.

I have noticed this behaviour on Windows Server 2008 R2 as well as later OSes. I am using 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
    }
}

1 Answer 1

1

The ISE will make things available (via autoloading) that you must explicitly initialize/call when running in the ISE. I.E., 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.

So, using the Output cmdlets, or variable squeezing (which assigns the results to a variable while simultaneously sending the results to the screen.)

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
    }
})
Sign up to request clarification or add additional context in comments.

2 Comments

wow.. mind=blown. Thanks. Even though I did not find the solutions intuitive, the variable squeezing method feels reasonably comfortable. But my original script contains enough code to make my intentions clear as to what has to happen but it in fact doesn't work the way it should makes me feel the "workaround" somehow goes against the original design principles of PowerShell. Again I could be wrong. Thanks again.
No worries. Things can be cloudy until to mess with them for a while. Those are just 3 options. Direct write output using the cmdlet, using an explicit Wirte-* command and that variable squeezing, assign the output to the variable while sending to the screen. This is my regular poor man's debugging approach so that I can see all results at each stage to make sure I am getting what I'd expect, without additional coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.