在 Powershell 中写入详细输出不会换行到命令宽度

发布于 2024-10-01 02:37:57 字数 367 浏览 1 评论 0原文

我想将大量数据Write-Verbose写入输出文件。我是这样做的。

Start-Transcript -Path $TargetDir\RunUnitTests.log -Width 1000000
Write-Verbose "five million character lines and stuff"

这非常有效,除了输出自动换行到控制台的标准宽度之外,这使得日志看起来非常糟糕。

我在这里找到了解决方案删除了死链接 ,但它是如此复杂和复杂,我不想只是将其放在我的脚本中的注释 #Thar be Dragons 下面。

有更好的方法吗?

I'd like to Write-Verbose a lot of data to an out file. Here's how I'm doing it.

Start-Transcript -Path $TargetDir\RunUnitTests.log -Width 1000000
Write-Verbose "five million character lines and stuff"

This works great, except that the output is auto wrapped to the standard width of a console, this makes the log look absolutely terrible.

I found a solution heredead link removed
, but it's so involved and complicated that I don't want to just throw this in my script below a comment #Thar be dragons.

Is there a better way to do this?

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

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

发布评论

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

评论(1

故乡的云 2024-10-08 02:37:57

这是一个基于 Write-Host 的非常简单的解决方法,它不存在这样的问题。在会话开始时 install/dot-source 替换默认的 Write-Verbose

function global:Write-Verbose
(
    [string]
    $Message
)
{
    # check $VerbosePreference variable
    if ($VerbosePreference -ne 'SilentlyContinue') {
        # do this via Write-Host
        Write-Host "VERBOSE: $Message" -ForegroundColor 'Yellow'
    }
}

然后这将根据需要工作:

$VerbosePreference = 'Continue'
Start-Transcript -Path .\RunUnitTests.log
Write-Verbose ("verbose writes five million character lines and stuff. " * 20)

也就是说:它考虑了 $VerbosePreference,它以黄色写入主机,转录输出未包装,并且仍标记为 VERBOSE。

**********************
Windows PowerShell Transcript Start
Start time: 20101105055855
**********************
Transcript started, output file is .\RunUnitTests.log
VERBOSE: verbose writes ... <long line text> ... and stuff.
**********************
Windows PowerShell Transcript End
End time: 20101105055855
**********************

Here is a very simple workaround based on Write-Host which does not have such a problem. In the beginning of the session install/dot-source the replacement of the default Write-Verbose:

function global:Write-Verbose
(
    [string]
    $Message
)
{
    # check $VerbosePreference variable
    if ($VerbosePreference -ne 'SilentlyContinue') {
        # do this via Write-Host
        Write-Host "VERBOSE: $Message" -ForegroundColor 'Yellow'
    }
}

Then this works as needed:

$VerbosePreference = 'Continue'
Start-Transcript -Path .\RunUnitTests.log
Write-Verbose ("verbose writes five million character lines and stuff. " * 20)

That is: it takes into account $VerbosePreference, it writes to host in yellow, transcript output is not wrapped and it is still marked VERBOSE.

**********************
Windows PowerShell Transcript Start
Start time: 20101105055855
**********************
Transcript started, output file is .\RunUnitTests.log
VERBOSE: verbose writes ... <long line text> ... and stuff.
**********************
Windows PowerShell Transcript End
End time: 20101105055855
**********************
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文