Answer a question

Any idea on how to use powershell to color my output on Jenkins ? I have already installed AnsiColor plugin on my Jenkins and I have set the job to use AnsiColor. The only problem is how to let my powershell output the color on Jenkins.

Answers

Well I've never used it before so I figured I'd try it. Basically you just literally put an escape character (ASCII 27) followed by a left bracket [ and then the codes, as described on this page, directly into the string.

To make this easier, I wrote a function that formats the string:

function Format-AnsiColor {
[CmdletBinding()]
[OutputType([String])]
param(
    [Parameter(
        Mandatory = $true,
        ValueFromPipeline = $true
    )]
    [AllowEmptyString()]
    [String]
    $Message ,

    [Parameter()]
    [ValidateSet(
         'normal display'
        ,'bold'
        ,'underline (mono only)'
        ,'blink on'
        ,'reverse video on'
        ,'nondisplayed (invisible)'
    )]
    [Alias('attribute')]
    [String]
    $Style ,

    [Parameter()]
    [ValidateSet(
         'black'
        ,'red'
        ,'green'
        ,'yellow'
        ,'blue'
        ,'magenta'
        ,'cyan'
        ,'white'
    )]
    [Alias('fg')]
    [String]
    $ForegroundColor ,

    [Parameter()]
    [ValidateSet(
         'black'
        ,'red'
        ,'green'
        ,'yellow'
        ,'blue'
        ,'magenta'
        ,'cyan'
        ,'white'
    )]
    [Alias('bg')]
    [String]
    $BackgroundColor
)

    Begin {
        $e = [char]27

        $attrib = @{
            'normal display' = 0
            'bold' = 1
            'underline (mono only)' = 4
            'blink on' = 5
            'reverse video on' = 7
            'nondisplayed (invisible)' = 8
        }

        $fore = @{
            black = 30
            red = 31
            green = 32
            yellow = 33
            blue = 34
            magenta = 35
            cyan = 36
            white = 37
        }

        $back = @{
            black = 40
            red = 41
            green = 42
            yellow = 43
            blue = 44
            magenta = 45
            cyan = 46
            white = 47
        }
    }

    Process {
        $formats = @()
        if ($Style) {
            $formats += $attrib[$Style]
        }
        if ($ForegroundColor) {
            $formats += $fore[$ForegroundColor]
        }
        if ($BackgroundColor) {
            $formats += $back[$BackgroundColor]
        }
        if ($formats) {
            $formatter = "$e[$($formats -join ';')m"
        }

       "$formatter$_"
    }
}

Usage:

Format-AnsiColor -Message 'Hey there' -Style Bold -ForegroundColor Red

'Hello' | Format-AnsiColor -BackgroundColor Green

'One','Two','Three' | Format-AnsiColor -Style 'normal display' -ForegroundColor White -BackgroundColor Black

Remember that you have to turn off the sequence (by that I mean set the style and colors back to whatever it was before) if you don't want it anymore.

Logo

CI/CD社区为您提供最前沿的新闻资讯和知识内容

更多推荐