请查看此测试脚本以及我对“ Receive-Job”如何工作的详细结论。
我还有问题要弄清楚,“ Receive-Job”是如何从代码块中提取流的。
<# .SYNOPSIS Test the console output and variable capturing of Write- cmdlet calls in a code block used by 'Start-Job'
.NOTES
.NET Version 4.7.2
PSVersion 5.1.16299.431
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.431
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
#>
Set-StrictMode -Version latest
if ($host.Name -inotmatch 'consolehost') { Clear-Host }
$errorBuffer = $null
$warningBuffer = $null
$outBuffer = $null
$infoBuffer = $null
# Start the job
$job = Start-Job -ScriptBlock {
Set-StrictMode -Version latest
PowerShell在其自己的进程中启动此脚本块,就像它将启动外部可执行文件一样。
因此,PowerShell只能将代码块中的stdout / success和stderr / error映射到脚本过程中PowerShell的成功(1)和错误(2)流。
这两个流将由Receive-Job
传递,并且可以按预期在Receive-Job
行中重定向。
Receive-Job
可以应要求将这两个流存储到变量中。 (-OutVariable -ErrorVariable
)
另外,Receive-Job
可以捕获PowerShell流信息(流6)和警告(流3),也可以将它们存储在变量中。 (-WarningVariable -InformationVariable
)
但是将这些流存储在变量中并不是重定向。
Write- cmdlet的每次调用都可以在控制台上显示一条消息,而与-swice变量无关。
控制台上的可见消息仅取决于Writecmdlet自己的首选项以及Write-cmdlet调用中可能的重定向。
# This will, by default, output to the console over stream 6 (info), and always get captured in $infoBuffer.
Write-Host "***WRITE_HOST***" # 6> $null # Supresses the output to the console.
# This will not output to the console over stream 6 (info) by default, but always get captured in $infoBuffer.
$InformationPreference = 'Continue' # Outputs to the console, default is 'SilentlyContinue'.
Write-Information "***INFO***" # 6> $null # Supresses the output to the console for preference 'Continue'.
$InformationPreference = "SilentlyContinue"
# This will not output to the console over stream 5 (debug) by default, and can't get captured in a variable.
$DebugPreference = 'Continue' # Outputs to the console, default is 'SilentlyContinue'.
Write-Debug "***DEBUG***" # 5> $null # Supresses the output to the console for preference 'Continue'.
$DebugPreference = "SilentlyContinue"
# This will not output to the console over stream 4 (verbose), by default, and can't get captured in a variable.
$VerbosePreference = 'Continue' # Outputs to the console, default is 'SilentlyContinue'.
Write-Verbose "***Verbose***" # 4> $null # Supresses the output to the console for preference 'Continue'.
$VerbosePreference = 'SilentlyContinue'
# This will, by default, output to the console over stream 3 (warning), but get captured in $warningBuffer only for
# preference 'Continue'.
#$WarningPreference = 'SilentlyContinue' # Supresses console output AND variable capturing, default is 'Continue'.
Write-Warning "***WARNING***" # 3> $null # Supresses the warning output to the console for preference
#$WarningPreference = 'Continue' # 'Continue'.
# This will output to the console over stream 2 (error), and always get captured in $errorBuffer, if not redirected
# in the code block.
# For 'Receive-Job -ErrorAction Stop' it would raise an execption, the content in $errorBuffer is quite useless then.
Write-Error '***ERROR***' # 2> $null # Supresses the output AND variable capturing, but you can supress/redirect
# this stream in the 'Receive-Job' line without breaking the variable
# capturing: 'Receive-Job ... -ErrorVariable errorBuffer 2> $null'
# These will output to the console over stream 1 (success), and always get captured in $result and $outBuffer, if
# not redirected in the code block.
Write-Output '***OUTPUT***' # 1> $null # Supresses the output AND variable capturing, but you can supress/redirect
Write-Output '***NEXT_OUTPUT***' # this stream in the 'Receive-Job' line without breaking the variable
"***DIRECT_OUT***" # capturing: '$result = Receive-Job ... -OutVariable outBuffer 1> $null'
}
# Wait for the job to finish
Wait-Job -Job $job
try
{
# Working only outside the code block, this is a workaround for catching ALL output.
#$oldOut = [Console]::Out
#$stringWriter = New-Object IO.StringWriter
#[Console]::SetOut($stringWriter)
# Pull the buffers from the code block
$result = Receive-Job <#-ErrorAction Stop#> `
-Job $job `
-ErrorVariable errorBuffer `
-WarningVariable warningBuffer `
-OutVariable outBuffer `
-InformationVariable infoBuffer `
# 1> $null #2> $null # Only the success and error streams can be redirected here, other
# streams are not available.
# Restore the console
#[Console]::SetOut($oldOut)
# Get all catched output
#$outputOfAllWriteFunctions = $stringWriter.ToString()
}
catch
{
Write-Host "EXCEPTION: $_" -ForegroundColor Red
}
finally
{
Write-Host "error: $errorBuffer"
Write-Host "warning: $warningBuffer"
Write-Host "out: $outBuffer"
Write-Host "info: $infoBuffer"
Write-Host "result: $result"
#Write-Host "`noutputOfAllWriteFunctions:`n";Write-Host "$outputOfAllWriteFunctions" -ForegroundColor Cyan
Remove-Job -Job $job
}
由于Start-Job
的代码块在其自己的进程中运行,因此无法直接写入脚本进程控制台。
该代码块由捕获机制包装,该机制捕获缓冲区中的所有6个PS流。
Receive-Job
的调用使用进程间通信来获取所有这些流。
Receive-Job
穿过流1和2,并使它们成为其自己的输出,因此可用于重定向。
Receive-Job
使用Write-Error
将流2写入控制台,因此Receive-Job
将引发参数-ErrorAction Stop
的异常。
然后Write-Error
使用Console.Out.WriteLine()
以红色写入控制台。
然后Receive-Job
检查变量存储并存储流1(成功),流2(错误),3(警告)和6(信息)。
最后,Receive-Job
使用Console.Out.WriteLine()
将具有不同ForegroundColors的流1、3、4、5和6写入控制台。
这就是为什么您可以使用Console.SetOut()
捕获所有这6个流输出,甚至包括错误流输出的原因,而我原本需要Console.SetError()
。
默认情况下,Write-Host
的输出将写入控制台,并将其输出添加到信息变量。
因此Write-Host
可能只是写入流6。
但是默认情况下,Write-Information
的输出在控制台上不可见,但也会添加到信息变量中。
因此,Write-Information
不能只与Write-Host
共享相同的IPC管道。
而且Write-Warning
可以独立地写入控制台和变量,因此这里也只能使用一个流/管道。
看看我关于这个问题的图表。
您可以通过重定向代码块中的流1-6和脚本中的流1或2来验证图。
|<-------- code block process -------->|<-- IPC -->|<-------------------- script process ------------------->|
Method Preference Stream Stream/Variable Console output
Write-Out * --> 1 --> PIPE 1 --> 1 --> Console.Out.Write(gray)
PIPE 1 --> Out Variable
Write-Error * --> 2 --> PIPE 2 --> 2 --> Console.Out.Write(red)
PIPE 2 --> Error Variable
Write-Warning Continue ----??????---> PIPE 3 --> Warning Variable
Write-Warning Continue --> 3 --> PIPE 4 --> Console.Out.Write(yellow)
Write-Verbose Continue --> 4 --> PIPE 4 --> Console.Out.Write(yellow)
Write-Debug Continue --> 5 --> PIPE 4 --> Console.Out.Write(yellow)
Write-Information Continue --> 6 --> PIPE 6 --> Console.Out.Write(gray)
Write-Information * ----??????---> PIPE 5 --> Information Variable
Write-Host * ----??????---> PIPE 5 --> Information Variable
Write-Host * --> 6 --> PIPE 6 --> Console.Out.Write(gray)
IPC : Inter Process Communication
* : always, independent from Preference or has no own Preference
您无法在Write-Information
或Write-Warning
之后添加重定向,以防止存储在其变量中。
如果您在方法之后重定向3和6,那么它将仅影响控制台输出,而不影响变量存储。
仅当$InformationPreference
(非默认值)或$WarningPreference
(默认值)设置为Continue(继续)时,它们才会写入流6或3,它们始终以灰色或黄色写入脚本进程的控制台。
并且只有Write-Warning
需要首选项Continue
才能存储在其变量中,Write-Informations
始终写入其变量。
问题:
- “写警告”和“写信息”如何在脚本处理过程中将其输出传递给分配的变量?
(由于Windows中不存在流,因此它们不能使用流7、8、9。)
在调用Job-Start
之后,您应该Start-Sleep
1-3秒,以使代码块有时间启动或失败。
然后,第一次使用Receive-Job
获取当前进度,启动调试信息,警告或错误。
您不应使用Wait-Job
,而应使用自己的循环来检查作业的运行状态并自行检查超时。
在该等待循环中,您每X秒钟调用Receive-Job
,以从代码块过程中获取进度,调试和错误信息。
当作业的状态为finished
或failed
时,您最后一次调用Receive-Job
以获取所有缓冲区的剩余数据。
要重定向/捕获流1(成功)和2(错误),您可以在Receive-Job
行中使用常规重定向或将其存储到变量中。
要捕获流3(警告)和流6(信息和Write-Host
),您必须使用变量存储。
您不能直接重定向或捕获流4(详细)或5(调试),但是可以将代码块中的这些流重定向(4>&1 or 5>&1
)到流1(成功)以将其添加到输出变量。
要抑制Write-Output
或Write-Error
的控制台输出,您只需在Receive-Job
行中重定向流1或2。
您不必限制Write-Information
,Write-Verbose
或Write-Debug
的控制台输出,因为它们不会使用默认首选项写入控制台。
如果要在分配的变量中捕获Write-Information
的输出而没有控制台输出,则必须重定向流6:Write-Information <message> 6>$null
。
要抑制Write-Warning
或Write-Host
的控制台输出,您必须在它们的调用行中重定向流3或6:Write-Warning <message> 3>$null
和Write-Host <message> 6>$null
。
如果在代码块中重定向流成功(1)或错误(2),它们将不会被传输到脚本进程,不会被写入控制台,也不会存储在输出或错误变量中。