It's possible to color hashtable column in powershell?

落爺英雄遲暮 提交于 2019-12-13 00:29:55

问题


It's possible to color single hashtable column in powershell?

For example color "Number" column to green:

Number VMNic
------ ----- 
[0]    vmnic0 
[1]    vmnic1 
[2]    vmnic2 
[3]    vmnic3 
[4]    vmnic4 
[5]    vmnic5 
[6]    vmnic6 
[7]    vmnic7

I create this output like that: [it's fragment from my code in the simplest way]

        $VMnics = get-vmhostnetworkadapter -Physical
        $NIC_collection = @()
        $VMnic_nr = "[$i]"
        Foreach ($VMnic in $VMnics){
                $NIC_Details = New-Object PSObject
                $NIC_Details | Add-Member -Name Number -Value $VMnic_nr -MemberType NoteProperty
                $NIC_Details | Add-Member -Name VMNic -Value $VMnic -MemberType NoteProperty
                [...]
                $NIC_collection += $NIC_Details
                $i++
             }
        $NIC_collection | Sort-Object VMNic | ft * -AutoSize

回答1:


You could hack it together with write-host commands, but it's not awesome. It means either manually formatting out columns, or regex matching and formatting matches. The key is that Write-Host allows you to specify colors, and it has -NoNewLine so you can string lines together.

Might there be a better way? Maybe, but I haven't found it, so if I wanted colored output to my host this is what I've done in the past.

This is pretty much what you had, but when you output the table I do it twice. The first time just lists the headers and the ---- under them. Then it skips those lines and it first converts the rest of it to strings, matches each line against the RegEx pattern "(\S+?\s+?)(\S+?\s*?)$" which means any non white-space characters, one or more, then white-space characters, one or more for the first matching group, followed by a second matching group of non white-space characters of one or more, and then any white-space characters (if any) until the end of the line. Then it writes the first matching group to the host as green, but does not create a new line, and then writes the second matching group to the host as dark yellow, and this time does put in a new line.

        $VMnics = get-vmhostnetworkadapter -Physical
        $NIC_collection = @()
        $VMnic_nr = "[$i]"
        Foreach ($VMnic in $VMnics){
                $NIC_Details = New-Object PSObject -Property @{
                    Number=$VMnic_nr
                    VMNic=$VMnic
                }
                #[...]
                $NIC_collection += $NIC_Details
                $i++
             }
        $NIC_collection | Sort-Object VMNic | ft * -AutoSize | Select -First 3
        $NIC_collection | Sort-Object VMNic | ft * -AutoSize | Select -Skip 3 | Out-String | ?{$_ -match "(\S+?\s+?)(\S+?\s*?)$"} | %{Write-Host $Matches[1] -ForegroundColor Green -NoNewline;Write-Host $Matches[2] -ForegroundColor DarkYellow}


来源:https://stackoverflow.com/questions/25186991/its-possible-to-color-hashtable-column-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!