I am using a .NET component in Powershell which uses Trace.TraceWarning
, Trace.TraceInformation
etc.
I want to output these traces to the console when I run my Powershell script.
This works when I use the component in the current session. For example (simulating the effect of trace) gives me 'Hello' output to the console:
$listener = new-object "system.diagnostics.consoletracelistener"
[System.Diagnostics.Trace]::Listeners.Add($listener) | Out-Null
[System.Diagnostics.Trace]::TraceInformation("Hello")
But if I do the same within a Powershell job I get no output, even though ConsoleTraceListener should be writing to STDOUT which in turn I expected to get captured by the job. (Interestingly Console.WriteLine
doesn't work from a job either - but Write-Host
does).
I am starting my job like so:
$work = {
$listener = new-object "system.diagnostics.consoletracelistener"
[System.Diagnostics.Trace]::Listeners.Add($listener) | Out-Null
[System.Diagnostics.Trace]::TraceInformation("Hello")
}
$job = Start-Job -RunAs32 -ScriptBlock $work
$job | Receive-Job -Wait