Search code examples
powershellrunspace

How to show runspace child powershell objects streams on host console?


I have some Powershell objects (created with [PowerShell]::Create()) acting as threads in my powershell application.

How can I show the streams data (Verbose and Error streams) on the invoker's console, during the run itself, and not only after the thread terminates?


Solution

  • $VerbosePreference needs to be set to "continue" on the thread environment. It can be also applied to the pipeline before the real execution script:

    $pipeline = [PowerShell]::Create()
    $pipeline.RunspacePool = $pool
    
    if ($PSBoundParameters['Verbose'].IsPresent) {
        $pipeline.AddScript({ $VerbosePreference = "Continue" }, $false).Invoke()
        $pipeline.Commands.Clear()
    }
    
    ... $pipeline execution code ...