在Powershell中不写入命令宽度的Write-Verbose输出

时间:2010-11-05 03:53:55

标签: logging powershell word-wrap

我想将Write-Verbose大量数据发送到out文件中。我就是这样做的。

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

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

我在这里找到了一个解决方案 dead link removed ,但它太复杂了,以至于我不想把它放在我的脚本中,而不是评论#Thar be dragons

有更好的方法吗?

1 个答案:

答案 0 :(得分:5)

这是一个基于Write-Host的非常简单的解决方法,它没有这样的问题。在会话开始时,安装/ 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
**********************