I am trying to understand how Receive-Job works internally. In below code I can see where Job object keeps records from different steams:
$InformationPreference = 'SilentlyContinue'
$sb = {
$VerbosePreference = 'Continue'
$InformationPreference = 'Continue'
$WarningPreference = 'Continue'
Write-Warning 'warning1'
Write-Information 'information1'
Write-Warning 'warning2'
Write-Information 'information2'
Write-Verbose 'verbose1'
Write-Information 'information3'
}
$job = Start-Job -ScriptBlock $sb | Wait-Job
# my messages are here:
$job.ChildJobs[0].Verbose.Count # prints 1
$job.ChildJobs[0].Information.Count # prints 3, only InformationRecord has TimeGenerated property
$job.ChildJobs[0].Warning.Count # prints 2
Receive-Job $job
# prints:
# WARNING: warning1
# information1
# WARNING: warning2
# information2
# VERBOSE: verbose1
# information3
But how I can write my own version of Receive-Job and keep original order of different messages? I tried to check source code, but it doesn't have a lot of sense:
private void WriteJobResults(Job job)
{
// ...
Collection<PSObject> output = ReadAll<PSObject>(job.Output);
foreach (PSObject o in output)
{
// ...
WriteObject(o);
}
Collection<ErrorRecord> errorRecords = ReadAll<ErrorRecord>(job.Error);
foreach (ErrorRecord e in errorRecords)
{
// ...
mshCommandRuntime.WriteError(e, true);
}
Collection<VerboseRecord> verboseRecords = ReadAll(job.Verbose);
foreach (VerboseRecord v in verboseRecords)
{
// ...
mshCommandRuntime.WriteVerbose(v, true);
}
// and so on for other streams...
}