Debugging Monad Scripts, Part 3: Write-Host

Did your command or script fail and/or report an error?  We hope to have a proper script debugger in a future version, but until then, MSH has some handy features to help you figure out what went wrong.  In this series of blog entries, I will present some of those features.  Thanks to Jim Truher [MSFT], Bruce Payette [MSFT], and Jeff Jones [MSFT] for their help in putting this together.

See the Windows "Monad" Shell Beta 2 Documentation Pack (https://www.microsoft.com/downloads/details.aspx?FamilyID=8a3c71d1-18e5-49d7-952a-c55d694ecee3&displaylang=en) for general information about Monad.

Part 1: https://blogs.msdn.com/monad/archive/2005/11/04/489138.aspx (Terminating vs. Non-Terminating Errors, ErrorRecord)
Part 2: https://blogs.msdn.com/monad/archive/2005/11/08/490130.aspx ($error)

Jon Newman [MSFT]

 

Write-Host

write-host is the "printf" (or "echo > /dev/tty" for *NIXers) of the Monad script debugging world. You can just put the debug output string in your script and it may appear in the output, but you don't want to do that. Consider

MSH C:\temp> get-content .\test2.msh
function doubleit($s)
{
"doubleit input was $s"
$s + $s
}
$out = doubleit "foo"
"doubleit output was " + $out
MSH C:\temp> .\test2.msh
doubleit output was doubleit input was foo foofoo
MSH C:\temp>

Because the script didn't use write-host, "doubleit input was foo" became part of the output of function doubleit. Be sure to use write-host (or write-warning, write-debug, or write-verbose) to generate printf-style output, so that the debug output is kept separate from the operation of your script:

MSH C:\temp> get-content .\test2.msh
function doubleit($s)
{
write-host "doubleit input was $s"
$s + $s
}
$out = doubleit "foo"
"doubleit output was " + $out
MSH C:\temp> .\test2.msh
doubleit input was foo
doubleit output was foofoo
MSH C:\temp>