【问题标题】:Changing color of selected rows from Format-Table in PowerShell从 PowerShell 中的 Format-Table 更改选定行的颜色
【发布时间】:2013-06-24 16:46:16
【问题描述】:

我正在尝试找到一种方法,使 Format-Table 以红色输出选定行,以绿色输出其他行。

我尝试做的一个例子是输出当前进程和它们正在使用的内存量。我想将逻辑应用于输出,以便使用超过给定内存量的进程将显示为黄色,而其他更高的进程将显示为红色。

我发现的最接近的问题是: Color words in powershell script format-table output

如何从字符串中提取数字进行处理?

【问题讨论】:

    标签: powershell colors powershell-2.0 output


    【解决方案1】:

    在 Powershell 中,处理非字符串对象通常更容易。但是,在此示例中,您必须使用 write-host cmdlet,因为它是唯一一个向控制台生成彩色输出的 cmdlet。但是 write-host 不会像大多数 cmdlet 那样格式化对象(使用 format.ps1xml 文件),因此要获得预期的格式,您可以通过管道将进程对象输出到字符串。但是 out-string 包括列标题和每行之前的空行(删除 substring() 调用以了解我的意思)。所以 substring(244,80) 只是获取实际进程信息有点麻烦。

    gps | % {
      $line = ($_|out-string).substring(244,80)
      if ($_.ws -lt 100MB) {
        write-host $line
      } elseif ($_.ws -lt 150MB) {
        write-host $line -foregroundcolor yellow
      } else {
        write-host $line -foregroundcolor red
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-10-19
      • 2018-04-12
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      相关资源
      最近更新 更多