This is a mock up of part of the code I'm working on
$output = @()
Start-ThreadJob -ScriptBlock {
try {
Write-Host "`nThis is a Write-Host message"
Write-Verbose 'This is a verbose message.' -Verbose
Start-Sleep 2
throw 'this is an error message'
}
catch {
Write-Host 'Error: ' $_.Exception.Message
}
} -ThrottleLimit 1 -StreamingHost $Host
I need to know if there is any way to redirect the output of the ThreadJob to a variable when using -StreamingHost $Host parameter. I don't want to use Receive-Job because that already works, I'm trying to see if it's possible to eliminate an extra step (and background task) by utilizing the -StreamingHost parameter of the Start-ThreadJob cmdlet.
Maybe it's possible to create a new host and then capture the data from there? I just need the string output of the thread job.
I don't want to modify the script block itself by redirecting the output inside of there.
I'm using PowerShell 7.5-beta