通过Powershell在詹金斯中使用AnsiColor [英] Use AnsiColor in jenkins via powershell
问题描述
关于如何使用Powershell为Jenkins上的输出着色的任何想法?我已经在我的Jenkins上安装了AnsiColor插件,并且已将作业设置为使用AnsiColor.唯一的问题是如何让我的Powershell在Jenkins上输出颜色.
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.
推荐答案
好吧,我之前从未使用过它,所以我想尝试一下.基本上,您只需在字面上放一个转义字符(ASCII 27),后跟左括号[
,然后输入代码中所述,noreferrer>直接插入字符串中.
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$_"
}
}
用法:
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.
这篇关于通过Powershell在詹金斯中使用AnsiColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!