Dar formato de colores a salida powershell

Leyendo el articulo de Kunal Udapi acerca de mostrar lineas pares e impares de distinto color de una salida powershell me vino la idea de resaltar aquellas lineas que cumplieran cierta condición.
Hicieron falta muy pocos cambios en el script original para cubrir mis necesidades.

get-service|format-highlight status stopped magenta

function Format-highlight {
    param ($param,$value,$color)

    begin {

        $ConsoleBack = [System.Console]::BackgroundColor
        $ConsoleFore = [System.Console]::ForegroundColor
        $RowColors = @(
            @{
                Back = $ConsoleBack
                Fore = $color
            },
            @{
                Back = $ConsoleBack
                Fore = $ConsoleFore
            }
        )
    }
    process {

        # $Index will be either 0 or 1 and used to pick colors from $RowColors
        if ($_.$param -eq $value){$index=0}else{$index=1}
        [System.Console]::BackgroundColor = $RowColors[$Index].Back
        [System.Console]::ForegroundColor = $RowColors[$Index].Fore
        $_
    }
    end {
        [System.Console]::BackgroundColor = $ConsoleBack
        [System.Console]::ForegroundColor = $ConsoleFore
    }

} #function Format-highlight
Recuerda añadir esta función a tu perfil de powershell en %userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 para tenerlo siempre disponible.

Comentarios