Output Issue with Transcript in Powershell
As I mentioned in my previous blog post, there is a "bug" in the current version of Powershell with the Transcript. For "external" commands, (that is programs that aren't Powershell commands) the transcript will log the input but not output. I first encountered this when I started calling "Robocopy" instead of "Copy-Item" because of the speed and features Robocopy provides over the native "copy-item" command.
My work around is very simple. I need to capture the output anyway to determine if there is an error. So I assign the output stream from Robocopy to a variable and then simply use "Write-Output" to display it in the stream. The drawback is that the interactive user is going to have to wait until the command has completed to see the output - but again, I need to parse the output for errors - so not much I can do about that.
Here's my code snippet:
using Robocopy for speed and more power with exlusion
"copying files with robocopy with the following command: $exec"
$exec="robocopy $sourcePath $destPath /XD _config* Deploy /s"
$out = Invoke-Expression $exec
#write the log to stream
Write-Output $out
#check if there was an error
$fail = $out -match "ERROR :"
if ($fail)
{
throw("Failure in deploying ProcessLauncher Files")
}
Another solution was provided by a reader to the blog Seb from New Zealand (thanks for reading Zeb!) which he got from the Microsoft Powershell Newsgroup: which is to use:
Function Fix-Transcript ()
{
Process{
Write-Host $_
}
}
ipconfig | Fix-Transcript
Stop-Transcript
While it’s a little “noisier” I think I prefer the Write-Output as I think it is a little clearer what’s happening.