Redirect Write-Host statements to a file

前端 未结 12 1754
鱼传尺愫
鱼传尺愫 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 08:48

    Until PowerShell 4.0, Write-Host sends the objects to the host. It does not return any objects.

    Beginning with PowerShell 5.0 and newer, Write-Host is a wrapper for Write-Information, which allows to output to the information stream and redirect it with 6>> file_name.

    http://technet.microsoft.com/en-us/library/hh849877.aspx

    However, if you have a lot of Write-Host statements, replace them all with Write-Log, which lets you decide whether output to console, file or event log, or all three.

    Check also:

    • Add-Content
    • redirection operators like >, >>, 2>, 2>, 2>&1
    • Write-Log
    • Tee-Object
    • Start-Transcript.
    0 讨论(0)
  • 2020-11-29 08:49

    You should not use Write-Host if you wish to have the messages in a file. It is for writing to the host only.

    Instead you should use a logging module, or Set/Add-Content.

    0 讨论(0)
  • 2020-11-29 08:49

    I have found the best way to handle this is to have a logging function that will detect if there is a host UI and act accordingly. When the script is executed in interactive mode it will show the details in the host UI, but when it is run via WinRM or in a non-interactive mode it will fall back on the Write-Output so that you can capture it using the > or *> redirection operators

    function Log-Info ($msg, $color = "Blue") {
        if($host.UI.RawUI.ForegroundColor -ne $null) {
            Write-Host "`n[$([datetime]::Now.ToLongTimeString())] $msg" -ForegroundColor $color -BackgroundColor "Gray"
        } else {
            Write-Output "`r`n[$([datetime]::Now.ToLongTimeString())] $msg"
        }
    }
    

    In cases where you want to capture the full output with the Write-Host coloring, you can use the Get-ConsoleAsHtml.ps1 script to export the host's scrolling buffer to an HTML or RTF file.

    0 讨论(0)
  • 2020-11-29 08:51

    Define a function called Write-Host. Have it write to a file. You may have some trouble if some invocations use a weird set of arguments. Also, this will only work for invocations that are not Snapin qualified.

    0 讨论(0)
  • 2020-11-29 08:56

    Use Write-Output instead of Write-Host, and redirect it to a file like this:

    Deploy.ps1 > mylog.log or Write-Output "Hello World!" > mylog.log
    
    0 讨论(0)
  • 2020-11-29 08:59

    Using redirection will cause Write-Host to hang. This is because Write-Host deals with various formatting issues that are specific to the current terminal being used. If you just want your script to have flexibility to output as normal (default to shell, with capability for >, 2>, etc.), use Write-Output.

    Otherwise, if you really want to capture the peculiarities of the current terminal, Start-Transcript is a good place to start. Otherwise you'll have to hand-test or write some complicated test suites.

    0 讨论(0)
提交回复
热议问题