【问题标题】:Powershell Standard Verbose to a variablePowershell标准详细到变量
【发布时间】:2016-12-02 12:34:55
【问题描述】:

我写了一个类似这样的脚本,我使用$output1 以便redirect 输出标准verbose

$output1 = $(
    cmdlet1 [Some Parameters] -Verbose | % {
        cmdlet2 [Other Some Parameters] -Verbose
    } ) 4>&1

当我不将标准详细信息存储在变量中时,它会以一种很好的方式在 powershell 中打印,并带有换行符。 但是当我在$output1 中捕获标准详细信息并将其保存在一个文件中时,它会将所有内容打印在一个非常长的单行中。

如何以一种好的方式将标准详细信息存储在我的变量中?

【问题讨论】:

    标签: powershell verbose


    【解决方案1】:

    据我所知,这没有简单的语法。为什么?因为管道输出否则会干扰详细、警告和错误消息。仍然有解决方法,稍微复杂一点。将您的详细输出重定向到临时文件,获取内容,删除文件。看看:

    $resultFile = [System.IO.Path]::GetTempFileName();
    1| % {
        $x = [System.IO.Path]::GetTempFileName();
        rm $x -Verbose
    } 4>$resultFile
    $result = cat $resultFile
    rm $resultFile
    

    在你的例子中:

    $resultFile = [System.IO.Path]::GetTempFileName();
    $(
    cmdlet1 [Some Parameters] -Verbose | % {
        cmdlet2 [Other Some Parameters] -Verbose
    }) 4>&1
    $output1 = cat $resultFile
    rm $resultFile
    

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 1970-01-01
      • 2019-09-23
      • 2013-02-22
      • 2017-05-27
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多