RICO Lee
RICO Lee

Reputation: 21

Need help on how to get the console output / return values as string

I need help on capturing the output messages or return values on console screen, and store them in variable and reuse them later. I need this to record the result of XMLA execution and error messages, though Powershell errors can be retrieved using $Error[0], I'd like to explore the possibility of capturing what is displayed on the console screen. I've found Start-Transcript but writing to a txt file does not seems too handy. Thanks in advance.

Upvotes: 2

Views: 525

Answers (1)

mjolinor
mjolinor

Reputation: 68341

If you run it as a background job, the output to the various data streams will be held in the buffers, and you can access each output stream of the child job individually:

$scriptblock = {
 $VerbosePreference = 'Continue'
 $DebugPreference = 'Continue'
 Write-Output "This is output"
 Write-Verbose "This is verbose"
 Write-Warning "This is a Warning"
 Write-Debug "This is Debug"
 Write-Error "This is an Error"
}

 $job = Start-Job -ScriptBlock $scriptblock
 Wait-Job -Job $job
 Receive-Job $job


 $job.ChildJobs[0].Output
 $job.ChildJobs[0].Verbose
 $job.ChildJobs[0].Warning
 $job.ChildJobs[0].Debug
 $job.ChildJobs[0].Error

Upvotes: 2

Related Questions