在下面摘录的我正在使用的函数中,有4个对Tee-Object
cmdlet的调用。前3个将消息根据需要写入控制台和日志。第四个使用Write-Error
,并且仅写入控制台。
# Run this step if the last step was successful...
if($script:BuildStepLastStatus -eq 0) {
Write-Output $("`n" + "-"*100) | Tee-Object -FilePath $script:BuildLogFileName -Append
Write-Output ("[{0:yyyy-MM-dd hh:mm:ss tt}]: Step {1}..." -f (Get-Date), $StepName) | Tee-Object -FilePath $script:BuildLogFileName -Append
try {
. $ScriptBlock | Tee-Object -FilePath $script:BuildLogFileName -Append
}
catch {
# Show the error details whether we will continue on error or not...
Write-Error "$($_)`n" | Tee-Object -FilePath $script:BuildLogFileName -Append
if($ContinueOnError.IsPresent) {
# 0 = success
$script:BuildStepLastStatus = 0
}
else {
# 1 = error condition
$script:BuildStepLastStatus = 1
}
}
}
如何修改此代码以将错误写入控制台和日志?
答案 0 :(得分:2)
AFAIK,Tee-Object
接受输出流中的内容,因此错误流中的错误将被忽略。您可以做相反的事情。
"$($_)`n" | Tee-Object -FilePath $script:BuildLogFileName -Append | Write-Error
答案 1 :(得分:1)