How do I add timestamps to individual lines of PowerShell & output?

后端 未结 4 1404
忘了有多久
忘了有多久 2020-12-29 05:11

How, if at all, is it possible to add timestamps to each line of an output generated by the & PowerShell operator?

Example:

PS H:\\&         


        
4条回答
  •  情深已故
    2020-12-29 05:12

    You could use the Start-Transcript cmdlet in combination with a custom prompt in your profile:

    # Configure PS prompt to show date and time
    function prompt
    { 
        $promptStringStart = "PS:" + (Get-Date -format MM/dd/yy` hh:mm:ss)
        $promptStringEnd += ">"
        Write-Host $promptStringStart -NoNewline -ForegroundColor Yellow
        Write-Host $promptStringEnd -NoNewline -ForegroundColor Yellow
        return " "
    }
    

    This works great on my Windows 7 workstation. However, on some newer Server 2012 R2 installations Start-Transcript seems to be slightly broken. This fixes part of it: https://support.microsoft.com/en-us/help/3014136/powershell-transcript-file-doesn-t-contain-the-correct-information-in-windows-server-2012-r2

    ... but it does not fix the issue with the custom prompt, still.

    For example, I see this on the console:

    PS:02/14/17 08:28:20> hostname
    server463
    

    And this is what's written to the log:

    PS:02/14/17 08:28:20
    >
    
    PS>hostname
    server463
    

提交回复
热议问题