{"id":10891,"date":"2006-04-25T12:17:51","date_gmt":"2006-04-25T12:17:51","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/debugging-monad-scripts-part-3-write-host\/"},"modified":"2019-02-18T13:25:06","modified_gmt":"2019-02-18T20:25:06","slug":"debugging-monad-scripts-part-3-write-host","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/debugging-monad-scripts-part-3-write-host\/","title":{"rendered":"Debugging Monad Scripts, Part 3: Write-Host"},"content":{"rendered":"<p>Did your command or script fail and\/or report an error?&nbsp; 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.&nbsp; In this series of blog entries, I will present some of those features.&nbsp; Thanks to Jim Truher [MSFT], Bruce Payette [MSFT], and Jeff Jones [MSFT] for their help in putting this together.<\/p>\n<p>See the Windows &#8220;Monad&#8221; Shell Beta 2 Documentation Pack (<a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=8a3c71d1-18e5-49d7-952a-c55d694ecee3&amp;displaylang=en\"><font color=\"#0000ff\">http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=8a3c71d1-18e5-49d7-952a-c55d694ecee3&amp;displaylang=en<\/font><\/a>) for general information about Monad.<\/p>\n<p>Part 1: <a href=\"http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/04\/489138.aspx\">http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/04\/489138.aspx<\/a>&nbsp;(Terminating vs. Non-Terminating Errors, ErrorRecord)<br \/>Part 2: <a href=\"http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/08\/490130.aspx\">http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/08\/490130.aspx<\/a>&nbsp;($error)<\/p>\n<p>Jon Newman [MSFT]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Write-Host<\/strong><\/p>\n<p>write-host is the &#8220;printf&#8221; (or &#8220;echo &gt; \/dev\/tty&#8221; 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&#8217;t want to do that. Consider<\/p>\n<p><font face=\"Courier New\"><\/p>\n<p>MSH C:\\temp&gt; get-content .\\test2.msh<br \/>function doubleit($s)<br \/>{<br \/>&#8220;doubleit input was $s&#8221;<br \/>$s + $s<br \/>}<br \/>$out = doubleit &#8220;foo&#8221;<br \/>&#8220;doubleit output was &#8221; + $out<br \/>MSH C:\\temp&gt; .\\test2.msh<br \/>doubleit output was doubleit input was foo foofoo<br \/>MSH C:\\temp&gt;<\/p>\n<p><\/font><\/p>\n<p>Because the script didn&#8217;t use write-host, &#8220;doubleit input was foo&#8221; 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:<\/p>\n<p><font face=\"Courier New\"><\/p>\n<p>MSH C:\\temp&gt; get-content .\\test2.msh<br \/>function doubleit($s)<br \/>{<br \/>write-host &#8220;doubleit input was $s&#8221;<br \/>$s + $s<br \/>}<br \/>$out = doubleit &#8220;foo&#8221;<br \/>&#8220;doubleit output was &#8221; + $out<br \/>MSH C:\\temp&gt; .\\test2.msh<br \/>doubleit input was foo<br \/>doubleit output was foofoo<br \/>MSH C:\\temp&gt;<\/font><font size=\"2\"><\/p>\n<p><\/font><\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did your command or script fail and\/or report an error?&nbsp; 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.&nbsp; In this series of blog entries, I will present some of those features.&nbsp; Thanks to Jim Truher [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Did your command or script fail and\/or report an error?&nbsp; 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.&nbsp; In this series of blog entries, I will present some of those features.&nbsp; Thanks to Jim Truher [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10891"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10891\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}