Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a generic way to capture all verbose output to a file but only show stdout on console?

Tags:

powershell

We have a bunch of different powershell tools that we use for build/deploy and other development and admin activity in my team. Mostly these are calling other Powershell scripts and Cmdlets but there are some x86 command line apps also (e.g. msbuild)

They largely all are setup to output verbose output. I do that so that I can troubleshoot retrospectively when something goes wrong in the team

However the team have asked to have less noisy output to the console. I still want the verbose output to be available retrospectively

So it feels like I need something like a continuous loop of the last 100k rows of verbose activity. Including any console input and output written to a file - regardless of the -verbose setting that the developer applied

Is there anything like this available in Powershell?

I know about redirection but I'm not sure how it can solve this problem as it seems that you have to match the stdout with the redirect even if you use Tee-Object - also it wouldn't capture inputs.

Eager to learn some hidden secrets or elegant creative solutions! :)

UPDATE: as mentioned redirect is not a practical solution. I've created a request https://github.com/PowerShell/PowerShell/issues/17482

like image 229
Brett Avatar asked Nov 27 '25 02:11

Brett


1 Answers

An article that addresses this topic is about_Redirection. Each output stream has a numeric identifier - the range is 1 to 6. The referenced article has a table showing the mapping between the ID's and streams.

Write-Host's ID is 6, so if you wanted everything to go to a log file except that, you can try redirecting all streams except 6:

PS:> Start-VeryNoiseyOperation -Debug 1>C:\mylogs\noisey.log 2>&1 3>&1 4>&1 5>&1

This example is redirecting all streams generated by the Write-____ cmdlets, except Write-Host and Write-Information. To send all streams to the log file, you can use *>C:\mylogs\noisey.log.

If you want to experiment, you can run this function with different combos of redirection to see the effect.

PS:> function Foo {
        [CmdletBinding()]param()
        Write-Output @{message = "my printed object"} # 1
        Write-Error   "This is an error message."     # 2
        Write-Warning "This is a warning message."    # 3
        Write-Verbose "This is a verbose message."    # 4
        Write-Debug   "This is a debug message."      # 5
        Write-Host    "This is a host message."       # 6
    }
PS:> $log     = New-TemporaryFile
PS:> $logPath = $log.FullName
PS:>
PS:> Foo -Debug -Verbose                 # Print everything.
       :
PS:> Foo -Debug -Verbose 1>$logPath      # Send object stream to file.
       :
PS:> Get-Content $logPath                # Object should print to console.
       :
PS:> Foo -Debug -Verbose 1>$logPath 2>&1 # Try different combos.
       :  # see effect on console output #
PS:> Get-Content $logPath
       :  # see log content #
like image 124
Todd Avatar answered Nov 30 '25 00:11

Todd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!