Receive-Job如何保持正确的记录顺序

时间:2019-05-19 15:08:03

标签: powershell powershell-jobs

我试图了解Receive-Job在内部如何工作。在下面的代码中,我可以看到Job对象将不同记录的记录保存在何处:

$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

但是如何编写自己的Receive-Job版本并保持不同消息的原始顺序?我试图检查源代码,但是没有太多意义:

https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/remoting/commands/ReceiveJob.cs

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...
}

1 个答案:

答案 0 :(得分:0)

正如@PetSerAl所述,我引用了错误的代码。这实际上是在执行:

// extract results and handle them
Collection<PSStreamObject> results = ReadAll<PSStreamObject>(job.Results);

if (_wait)
{
    foreach (var psStreamObject in results)
    {
        psStreamObject.WriteStreamObject(this, job.Results.SourceId);
    }
}
else
{
    foreach (var psStreamObject in results)
    {
        psStreamObject.WriteStreamObject(this);
    }
}

很遗憾,Results是内部属性。我研究了它的工作原理,下面是代码:

$nonPublicInstance = [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance
$JobResultsProperty = $job.GetType().GetProperty('Results', $nonPublicInstance)

$results = $JobResultsProperty.GetValue($job.ChildJobs[0])

$PSStreamObjectValueProperty = [System.Management.Automation.Remoting.Internal.PSStreamObject].GetProperty('Value', $nonPublicInstance)

foreach ($result in $results)
{
    $value = $PSStreamObjectValueProperty.GetValue($result)
    switch ($result.ObjectType)
    {
        'Verbose' {
            Write-Output "Verbose Record: $value"
        }
        'Information' {
            Write-Output "Information Record: $value"
        }
        'WarningRecord' {
            Write-Output "Warning Record: $value"
        }
        'MethodExecutor' {
            Write-Output "MethodExecutor"
        }
    }
}

由于某种原因ObjectType=Verbose不在收藏中。我假定以某种方式从MethodExecutor记录中提取了详细记录。输出:

MethodExecutor
Warning Record: warning1
MethodExecutor
Information Record: information1
MethodExecutor
Warning Record: warning2
MethodExecutor
Information Record: information2
MethodExecutor
MethodExecutor
Information Record: information3
MethodExecutor

我找到了一种从工作中获得结果的更好方法,但是它仅适用于新创建的工作。以下代码在集合上为DataAdded事件添加了事件处理程序:

$job = Start-Job -ScriptBlock $sb

$records = New-Object 'System.Collections.Generic.List[System.String]'

Register-ObjectEvent -InputObject $job.ChildJobs[0].Verbose -EventName 'DataAdded' -Action { $records.Add('verbose: ' + $Sender[$EventArgs.Index]) }.GetNewClosure() | Out-Null
Register-ObjectEvent -InputObject $job.ChildJobs[0].Information -EventName 'DataAdded' -Action { $records.Add('information: ' + $Sender[$EventArgs.Index]) }.GetNewClosure() | Out-Null
Register-ObjectEvent -InputObject $job.ChildJobs[0].Warning -EventName 'DataAdded' -Action { $records.Add('warning: ' + $Sender[$EventArgs.Index]) }.GetNewClosure() | Out-Null

Wait-Job $job | Out-Null

$records | Format-List

输出:

warning: warning1
information: information1
warning: warning2
information: information2
verbose: verbose1
information: information3