powershell脚本格式表输出中的颜色字

powershell脚本格式表输出中的颜色字,powershell,powershell-2.0,Powershell,Powershell 2.0,是否可以使用格式表仅为powershell输出的某些字(非完整行)上色。例如,此脚本递归扫描文件夹中的字符串,然后使用format table输出结果 dir -r -i *.* | Select-String $args[0] | format-table -Property @{label="Line #"; Expression={$_.LineNumber}; width=6}, Path, Line -wrap 如果能够将我们正在搜索的单词格式化为特定颜色,这样您就可以准确地看到它在

是否可以使用格式表仅为powershell输出的某些字(非完整行)上色。例如,此脚本递归扫描文件夹中的字符串,然后使用format table输出结果

dir -r -i *.* | Select-String $args[0] |
format-table -Property @{label="Line #"; Expression={$_.LineNumber}; width=6},
Path, Line -wrap

如果能够将我们正在搜索的单词格式化为特定颜色,这样您就可以准确地看到它在行中的位置,那将是一件好事。

您可以通过管道将表格导入
输出字符串
,然后使用
write Host
-NoNewLine
开关将字符串分部分写入

大概是这样的:

filter ColorWord {
    param(
        [string] $word,
        [string] $color
    )
    $line = $_
    $index = $line.IndexOf($word, [System.StringComparison]::InvariantCultureIgnoreCase)
    while($index -ge 0){
        Write-Host $line.Substring(0,$index) -NoNewline
        Write-Host $line.Substring($index, $word.Length) -NoNewline -ForegroundColor $color
        $used = $word.Length + $index
        $remain = $line.Length - $used
        $line = $line.Substring($used, $remain)
        $index = $line.IndexOf($word, [System.StringComparison]::InvariantCultureIgnoreCase)
    }
    Write-Host $line
}

Get-Process| Format-Table| Out-String| ColorWord -word 1 -color magenta
我喜欢他的方法。下面是一个替代实现,使用而不是:

如果行以给定单词开头或结尾,Split包含空字符串,因此额外的“if not first”逻辑


Edit:在Rynant的评论之后,下面是另一个支持simple和regex模式的实现:

filter ColorPattern( [string]$Pattern, [ConsoleColor]$Color, [switch]$SimpleMatch ) {
  if( $SimpleMatch ) { $Pattern = [regex]::Escape( $Pattern ) }
  
  $split = $_ -split $Pattern
  $found = [regex]::Matches( $_, $Pattern, 'IgnoreCase' )
  for( $i = 0; $i -lt $split.Count; ++$i ) {
    Write-Host $split[$i] -NoNewline
    Write-Host $found[$i] -NoNewline -ForegroundColor $Color
  }
  
  Write-Host
}
以下示例的输出显示了差异:

PS>“\d00\d!”|颜色模式'\d''洋红色'-Simple

\d00\d

PS>“\d00\d!”|彩色图案'\d''洋红色'

\d00\d


我喜欢赖恩特给出的答案。我这里有一个修改版本,可以通过传递数组或单词和颜色来为输出中的多个单词着色。诀窍在于必须根据换行符将输入文本拆分为行

filter ColorWord2 {
param(
    [string[]] $word,
    [string[]] $color
)
$all = $_
$lines = ($_ -split '\r\n')

$lines | % {
    $line = $_      
    $x = -1

    $word | % {
        $x++
        $item = $_      

        $index = $line.IndexOf($item, [System.StringComparison]::InvariantCultureIgnoreCase)                            
            while($index -ge 0){
                Write-Host $line.Substring(0,$index) -NoNewline                 
                Write-Host $line.Substring($index, $item.Length) -NoNewline -ForegroundColor $color[$x]
                $used =$item.Length + $index
                $remain = $line.Length - $used
                $line =$line.Substring($used, $remain)
                $index = $line.IndexOf($item, [System.StringComparison]::InvariantCultureIgnoreCase)
            }
        }

    Write-Host $line
} }
并将按如下方式执行

Get-Service | Format-Table| Out-String| ColorWord2 -word 'Running','Stopped' -color 'Green','Red'

很好!修改了这一行:$index=$line.IndexOf($word)到$index=$line.IndexOf($word,[System.StringComparison]::invariantCultureInogoreCase)忽略大小写很好。我已经用修改更新了答案。请注意,您需要转义正则表达式字符。例如,
“Ke$ha-Tik Tok.mp3”|颜色词“Ke\$ha”红色
。或者,过滤器中可以使用
$\u-split[regex]::Escape($word)
。除非你想和正则表达式匹配,这可能很好。@Rynant谢谢你指出这一点!更新了新版本。我喜欢这个答案,这正是我想要的。干杯。我现在用了一个变体。我没有传入两个数组,而是传入一个哈希表,颜色是键,值是将应用颜色的单词数组。
Get-Service | Format-Table| Out-String| ColorWord2 -word 'Running','Stopped' -color 'Green','Red'