Redirect Write-Host statements to a file

前端 未结 12 1763
鱼传尺愫
鱼传尺愫 2020-11-29 08:39

I have a PowerShell script that I am debugging and would like to redirect all Write-Host statements to a file. Is there an easy way to do that?

12条回答
  •  眼角桃花
    2020-11-29 09:12

    This worked for me in my first PowerShell script that I wrote few days back:

    function logMsg($msg)
    {
        Write-Output $msg
        Write-Host   $msg
    }
    

    Usage in a script:

    logMsg("My error message")
    logMsg("My info message")
    

    PowerShell script execution call:

    ps> .\myFirstScript.ps1 >> testOutputFile.txt
    

    It's not exactly answer to this question, but it might help someone trying to achieve both logging to the console and output to some log file, doing what I reached here :)

提交回复
热议问题