Hi,
I am busy scripting a system in PowerShell that allows us to run groups of scripts against remote servers easily with little scripting knowledge from the end user. I am running into a problem where when using PS-Remoting and PS-Automation Jobs, I cannot capture the output and act on it in a variable.
Here is a simple example of what is happening:
PS C:\> $ScriptBlock = { write-host "hi" }
PS C:\> $Job = Start-Job -ScriptBlock $ScriptBlock
PS C:\> $JobOutput = Receive-Job -Job $Job
hi
PS C:\> $JobOutput -eq $null
True
I am trying to capture the output (in this case, the string "hi") and act on it in some way. But in this case the output is piped directly to the console and I am left with a null variable.
I know about this workaround:
PS C:\> $ScriptBlock = { powershell.exe -Command { write-host "hi" } }
PS C:\> $Job = Start-Job -ScriptBlock $ScriptBlock
PS C:\> $JobOutput = Receive-Job -Job $Job
PS C:\> $JobOutput
hi
However if I do it that way I can only get output after the script finishes executing, which is not what I want. Some of the scripts run for many minutes and log a lot. I have no control over the contents of the scripts.
Anyone have any suggestions? I thought of using tee-object or trying to use some other pipeline operations but I have had no success at all.
Edit: I changed the title of the post to more reflect what I am trying to do.