本文介绍了Write-Verbose输出不会在Powershell中包装为命令宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想Write-Verbose将大量数据保存到一个out文件中.这就是我的做法.

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.

我在这里找到了解决方法,但是它是如此复杂且复杂,以至于我不想在脚本中的注释#Thar be dragons下方加上它.

I found a solution here, 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.

有更好的方法吗?

推荐答案

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

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)

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

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
**********************

这篇关于Write-Verbose输出不会在Powershell中包装为命令宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:31