【问题标题】:How to colorise PowerShell output of Format-Table如何为 Format-Table 的 PowerShell 输出着色
【发布时间】:2013-12-20 13:50:36
【问题描述】:

如果值大于 100 MB,我会尝试将列 RAM 着色为红色:

Get-Process | Format-Table @{ Label = "PID"; Expression={$_.Id}},
            @{ Label = "Name"; Expression={$_.Name}},
            @{ Label = "RAM (MB)"; Expression={[System.Math]::Round($_.WS/1MB, 1)}},
            @{ Label = "Responding"; Expression={$_.Responding}}

我尝试使用 Write-Host -nonewline,但结果错误。

Get-Process | Format-Table @{ Label = "PID"; Expression={$_.Id}},
            @{ Label = "Name"; Expression={$_.Name}},
            @{ Label = "RAM (MB)"; Expression={write-host -NoNewline $([System.Math]::Round($_.WS/1MB, 1)) -ForegroundColor red}},
            @{ Label = "Responding"; Expression={ write-host -NoNewline $_.Responding -fore red}}

【问题讨论】:

  • 结果如何?请张贴截图。
  • 阅读这篇文章,可能会有所帮助powershellmagazine.com/2013/06/20/…
  • 我提供了一个答案,但它不给列着色,只是给行着色。
  • 您可以对列进行不同的着色,接受的答案不再正确,您需要使用 Write-PSObject。请参阅下面的答案。

标签: powershell colors console


【解决方案1】:

从 PowerShell 5.1 或更高版本开始,您可以使用 VT escape sequences 将颜色添加到单个列,但前提是您的控制台支持 VT 转义序列(例如 Windows 10 Fall Creators Update、Linux 或 Mac,但不支持 Windows 8 w/ o ConEmu 等控制台模拟器)。

这是一个在表达式中指定格式的示例,但同样可以在 ps1xml 文件中使用:

dir -Exclude *.xml $pshome | Format-Table Mode,@{
    Label = "Name"
    Expression =
    {
        switch ($_.Extension)
        {
            '.exe' { $color = "93"; break }
            '.ps1xml' { $color = '32'; break }
            '.dll' { $color = "35"; break }
           default { $color = "0" }
        }
        $e = [char]27
       "$e[${color}m$($_.Name)${e}[0m"
    }
 },Length

结果输出,注意列宽看起来不错,转义序列字符中没有多余的空格。

【讨论】:

【解决方案2】:

接受的答案不正确,可以对列进行着色。获取条件列颜色的解决方案是使用Write-PSObject

Here are some wonderful examples 带有文档化的代码和解释。

来自上述资源:

Write-PSObject $servers -MatchMethod Exact -Column "Manufacture" -Value "HP" -ValueForeColor Yellow -ValueBackColor Red -RowForeColor White -RowBackColor Blue;

我是通过a GitHub issue to add color formatting to Format-Table 发现的,这似乎是 PowerShell 开发人员希望在某个时候添加的功能。

【讨论】:

    【解决方案3】:

    您可以使用正则表达式为着色...

    filter colorize-row{
    
        Get-Process | Select-Object Id, Name, WS, Responding | foreach {
    
            # Print 'red' row if WS greater than 100 MB
            if([System.Math]::Round($_.WS/1MB,1) -match "^([0-9]|[0-9][0-9]|[1-9][0-9]?$|^100$)$"){
                [console]::ForegroundColor="white"; $_;
            } else {
                [console]::ForegroundColor="red"; $_;
            }
        }
    }
    
    colorize-row
    

    输出:

    【讨论】:

    • 有没有办法像使用Format-Table时那样格式化?我尝试了@{n="<heading>"; e={$_.<value>}; a="left"} 语法,但它不起作用。
    • 这个答案不再正确,你可以给列着色,看我的答案。
    【解决方案4】:

    快速的回答是你不能。可以将Write-Host 与颜色一起使用,但没有“输出”可以发送到格式表。

    Write-Host 的“输出”是一种副作用,它将数据直接发送到控制台,而不是像标准函数那样将数据返回给调用者。

    结合@David Martin 的评论,这里的a link 具有有趣的模式匹配格式颜色功能。

    【讨论】:

      【解决方案5】:

      你可以安装模块PsWrite

      Write-color

      他还有其他智能功能,比如

      Write-logstep

      Write-ProgressbarreColor

      【讨论】:

        【解决方案6】:

        是的,您可以使用 ANSI Escape 颜色并设置有条件的颜色:

        Get-Process | Format-Table @{ Label = "PID"; Expression={$_.Id}},
                @{ Label = "Name"; Expression={$_.Name}},
                @{ Label = "RAM (MB)"; Expression={if($_.WS/1MB -gt 100){"$([char]27)[0;31m$([System.Math]::Round($_.WS/1MB, 1))$([char]27)[0m"}else{"$([char]27)[0;32m$([System.Math]::Round($_.WS/1MB, 1))$([char]27)[0m"}}},
                @{ Label = "Responding"; Expression={if($_.Responding -eq $true){"$([char]27)[0;32m$($_.Responding)$([char]27)[0m"}else{"$([char]27)[0;31m$($_.Responding)$([char]27)[0m"}}}
        

        【讨论】:

          猜你喜欢
          • 2021-11-28
          • 1970-01-01
          • 2021-10-25
          • 1970-01-01
          • 1970-01-01
          • 2010-11-29
          • 2011-01-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多