PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 16, 2022

[FIXED] How can I capture the Information output stream of a Job?

 August 16, 2022     jobs, output, powershell     No comments   

Issue

If I start a job, and that job outputs messages to the Information stream (or any stream for that matter), how can I capture that into a variable, or simply output it after I receive the job?

$script = {
    [PSCustomObject]@{ "CoolProperty" = "CoolValue" }
    Write-Information "Returning a cool object" -InformationAction "Continue"
}

Start-Job -Name "test" -ScriptBlock $script | Out-Null
Wait-Job -Name "test" | Out-Null
$result = Receive-Job -Name "test" 6>&1
Remove-Job -Name "test"

The above code will output

Returning a cool object

to the console, but I would like to capture that and output it to the log file I'm using for the overall script, e.g.:

$JustTheInfoStreamFromJob | Out-File "c:\file.log" -Append

I don't want to log the captured $result, because it also contains the Output stream (i.e. the object). I only want to log the Information stream. So I'm looking for a way to separate that out.

I see that there is an -InformationVariable parameter, but I don't understand how to use it, or if it's relevant for my question. I've tried a few methods of redirection, but I have very little idea what I'm doing when it comes to streams.

This question gives me some hints, but not enough to understand.

This question is very similar but appears to have been asked before the Information stream existed. I'd rather not use Transcripts unless it's necessary; I feel like there should be a better way.

Some answers reference the ChildJobs, Output, and Information properties of a Job object, but I'm having a hard time understanding how to use them, as they are always null in my simple tests.

Thanks for your time.

Solved

Thanks to @Santiago Squarzon and @Steven for two different working solutions:

Remove-Job "test"
$script = {
    [PSCustomObject]@{ "CoolProperty" = "CoolValue" }
    Write-Information "Returning a cool object"
}

Start-Job -Name "test" -ScriptBlock $script | Out-Null
$job = Get-Job -Name "test" -IncludeChildJob # Steven's solution
$job | Wait-Job | Out-Null
$info = $job.Information # Steven's solution
$result = Receive-Job -Name "test" -InformationVariable info2 # Santiago Squarzon's solution
Remove-Job -Name "test"

$result
"---"
$info
"---"
$info2

This allows me to capture the job's output and the job's Info stream separately (two different ways):

CoolProperty RunspaceId
------------ ----------
CoolValue    f49c78bd-eda3-4c47-a6bc-d89a146618e9
---
Returning a cool object
---
Returning a cool object

Solution

The different Streams are stored separately in the job object:

State         : Completed
HasMoreData   : True
StatusMessage :
Location      : localhost
Command       :  Write-Information "something"
JobStateInfo  : Completed
Finished      : System.Threading.ManualResetEvent
InstanceId    : ff5d1155-aca1-40fa-8e4e-dce6b87c709c
Id            : 2
Name          : test
ChildJobs     : {Job3}
PSBeginTime   : 4/2/2021 9:41:26 PM
PSEndTime     : 4/2/2021 9:41:26 PM
PSJobTypeName : BackgroundJob
Output        : {}
Error         : {}
Progress      : {}
Verbose       : {}
Debug         : {}
Warning       : {}
Information   : {}

Notice the child Job, but you can get at the information stream with something like:

(Get-Job -Name test -IncludeChildJob).Information

In the above example it will return "something", reference the command from the job.

Reference here there's some other good information as well.



Answered By - Steven
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing