Freigeben über


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>

Comments

  • Anonymous
    November 17, 2005
    Problem with msh script and redirection.

    I cannot get redirection of stdout and stderr work for my monad scripts..

    Example (put these 4 lines into an .msh file)
    write-host "testing"
    write-host "testing"
    net.exe config # or any win32 prog
    write-host "testing"

    Running this produces nice output as expected.
    But running this from a cmd prompt with redirection of stdout and stderr:
    >type t.log
    testing
    testing
    The following running services can be controlled:

    Server
    Workstation

    The command completed successfully.

    write-host : The OS handle's position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win32 code or another FileStream. This may cause data loss.
    At C:temptest.msh:4 char:11
    + write-host <<<< "testing"

    >
    And the script terminates. Maybe as expected but I need to work around it.

    All I want to accomplish is to launch my monad scripts from e.g. 'at' and get all output into a logfile. How can I do this.
    Have tried to do [Console]::SetOut but nothing happens.
    (Don't know if this is the correct place/forum.. pls advice)
    ///Stefan Lundberg
  • Anonymous
    November 17, 2005
    The comment has been removed
  • Anonymous
    November 17, 2005
    Thanks. We're looking into it.

    Jon Newman [MSFT]
  • Anonymous
    April 25, 2006

    Did your command or script fail and/or report an error?&amp;nbsp; We hope to have a proper script debugger...
  • Anonymous
    April 25, 2006

    Did your command or script fail and/or report an error?&amp;nbsp; We hope to have a proper script debugger...
  • Anonymous
    April 25, 2006

    Did your command or script fail and/or report an error?&amp;nbsp; We hope to have a proper script debugger...
  • Anonymous
    April 25, 2006

    Did your command or script fail and/or report an error?&amp;nbsp; We hope to have a proper script debugger...
  • Anonymous
    June 24, 2007
    So why do you care about debugging powershell scripts? Umm unless you have the superhuman ability to