Someone posted a note to me on Twitter that noted that Write-Host is not recommended for your scripts. I checked the link to an MSDN blog on Write-Host Considered Harmful, and it made some sense. Basically it says that since Write-Host always goes to the console, any output sent through Write-Host can’t be consumed in a pipeline by other PoSh commandlets or processes.
At first I thought, what does that have to do with my script? I’m really just noting status information. However, the more I thought about it, the more I realized that it’s a minor change, and who knows? Maybe I’ll chain this in some other process, or more importantly, maybe someone else will.
Today I popped open the script in the PowerShell ISE and did this:
That’s an easy fix. Just write the output to the pipeline, and if there’s nothing consuming output, I get it on the screen.
I also refactored a bit more. I added a “Debug x:” line to each Write-Output command, with x replaced by the appropriate debug level I’d checked for. This way I know what debugging output is being returned to the calling screen.
I also found a few lines that were just output, using “Attaching as…” code. I replaced those with Write-Output.
Filed under: Blog Tagged: administration, Backup/Recovery, powershell, sql server, syndicated