【问题标题】:Change color of output in powershell在powershell中更改输出颜色
【发布时间】:2015-02-25 20:53:06
【问题描述】:

尝试查看是否可以使用以下几行更改输出的颜色。请注意,这不是完整的代码,只是其中的一部分:

$ADInfo `
    | Format-List `
        @{ Name = "Full Name"; Expression = { $_.name } },
        @{ Name = "Display Name"; Expression = { $_.displayname } },
        @{ Name = "User ID"; Expression = { $_.samaccountname } },
        @{ Name = "Email"; Expression = { $_.mail } },
        @{ Name = "Exchange Version"; Expression = { $_.msExchVersion } },

如果可能的话,我想单独更改名称和输出的颜色。

输出:

【问题讨论】:

  • write-host -NoNewLine 的组合可以帮助解决这个问题。可能具有复杂的逻辑。你的着色条件是什么?
  • 你想colour each row吗?某些元素?一整列?很高兴知道是哪一个。
  • 对不起,你确实有你的条件。可以肯定的是,您可以复制您的输出作为示例吗?
  • 就像@Matt 所说,这可以通过 Write-Host 和 -NoNewLine 参数来完成。查看我在this other SO question 中的回答,了解如何执行此操作的示例。
  • @Matt 我为此添加了输出截图。如果可能的话,我想尝试独立更改任一列的颜色。所以也许改变“电子邮件”这个词的颜色和它旁边的输出,“学生”是不同的颜色。希望这有助于解决问题。

标签: powershell colors output


【解决方案1】:
Function Write-HostColoured{
    [cmdletbinding()]
    Param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        $Input,
        [parameter(Mandatory=$true)]
        [string]$ColouredProperty,
        [System.ConsoleColor]$Colour = "Red"

    )

    # Get all the note properties of the object which we need for output and determining conditional formatting.
    $properties = $input | gm -MemberType NoteProperty | Select-Object -ExpandProperty name

    # Find the property with the longest length. Used for aligning output. 
    $longestProperty = $properties | ForEach-Object{$_.Length} | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum

    # Check if the property to colour requested is in $properties
    If($properties -notcontains $ColouredProperty){Write-Warning "Input object does not contain the property $ColouredProperty"}

    # Insert Line break between objects. 
    Write-Host

    ForEach($singleObject in $Input){
        $properties | ForEach-Object{
            Write-Host ("{0,-$longestProperty} : " -f $_) -NoNewline

            # Check if the property requires a colour formatting
            If($_ -eq $ColouredProperty){
                Write-Host $singleObject.$_ -ForegroundColor $Colour
            } Else {
                Write-Host $singleObject.$_
            }
        }

        # Insert Line break between objects. 
        Write-Host
    }
}

Import-Csv C:\temp\data.csv -header "Col123fff1","cl2","col3","col4" | Write-HostColoured -ColouredProperty Col3

样本输出

说明

认为这需要根据您的评论进行改进,但让我们从这个开始。它将从一个对象(使用Import-CSV 测试)获取管道输入,然后模拟Format-List 的输出。在使用Write-Host 输出时,如果正在处理的属性与$ColouredProperty 匹配,则应用$colour 定义的前景色。

这目前仅在您需要为一列着色时才有效,但我可以看到这也为条件格式设置了哈希表输入。

如果我们走在正确的轨道上,请告诉我。

条件着色

如果您更改 if 语句并添加另一个子句,您可以轻松地根据条件使着色成为条件

If($_ -eq $ColouredProperty -and $singleObject.$_ -match "\d")

它使函数的可转移性降低,但它会起作用。我用它来突出显示所有带有数字的文件名。

Get-ChildItem C:\temp -filter *.txt | Select Name,Length,Directory | Write-HostColoured -ColouredProperty Name    

【讨论】:

  • 我想是的。我对 powershell 还是很陌生,但如果它匹配某个字符串,我有兴趣为输出着色。如果不是太复杂的话。
  • @Aaron。认为那将是你要去的方向。这将需要 一点 更多的努力,但我认为我们可以做到(我已经在脑海中转动轮子了。)。列名是否需要突出显示?
  • 如果太难了,不。但是我得到的一些值就像....“帐户锁定?:真/假”..所以我想要 True 或 False 颜色的原因是更容易发现。
  • @Aaron 您是否希望着色只是有条件的?或者你想给行上色吗?还是两者兼而有之?
  • 取决于难度。我想了解两者以了解它是如何工作的,但我可能会坚持最终将输出着色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 2021-12-04
  • 2021-05-04
  • 2021-12-28
  • 1970-01-01
  • 2019-07-26
相关资源
最近更新 更多