是否有一种通用方法可以将所有详细输出捕获到文件中,但仅在控制台上显示Stdout?

发布于 2025-01-23 16:20:27 字数 571 浏览 0 评论 0原文

我们有许多不同的PowerShell工具,用于在我的团队中用于构建/部署以及其他开发和管理活动。通常,这些都调用其他PowerShell脚本和CMDLET,但也有一些X86命令行应用程序(例如MSBUILD),

它们在很大程度上都已设置为输出冗长输出。我这样做是为了使我可以在团队中出现问题时进行回顾性问题,

但是团队要求对游戏机的嘈杂输出更少。我仍然希望回顾性的详细输出可用

,因此感觉就像我需要最后100k行冗长活动的连续循环。包括任何写入文件的控制台输入和输出 - 不管开发人员应用的 -

powershell中是否有类似此类的验证设置?

我知道重定向,但是我不确定它如何解决此问题,因为即使您使用Tee -Object,您也必须将Stdout与重定向匹配 - 也不会捕获输入。

渴望学习一些隐藏的秘密或优雅的创意解决方案! :)

更新:如前所述,重定向不是实用的解决方案。我已经创建了一个请求

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2025-01-30 16:20:27

解决此主题的一篇文章是 about_redirection 。每个输出流都有一个数字标识符 - 范围是1到6。引用的文章的表显示了ID和流之间的映射。

写入主持人的ID是6,因此,如果您希望所有内容都可以转到日志文件,则可以尝试重定向所有流,除6:

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

此示例是重定向写入 - ____ cmdlet,除写入 - 宿主写入信息外。要将所有流发送到日志文件,您可以使用*> c:\ mylogs \ noyey.log

如果您想实验,则可以通过不同的重定向组合运行此功能,以查看效果。

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 #

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 #
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文