In the following excerpt from a function that I am working on there are 4 calls to the Tee-Object cmdlet. The first 3 write the messages to the console and the log as I wanted. The 4th one uses Write-Error and it only writes to the console.
# 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
}
}
}
How can I modify this code to write errors to the console and the log?