关于如何使用powershell为Jenkins上的输出着色的任何想法?我已经在我的Jenkins上安装了AnsiColor插件,我已经设置了使用AnsiColor的工作。唯一的问题是如何让我的powershell在Jenkins上输出颜色。
答案 0 :(得分:3)
我在Jenkin中使用了很多长的powershell,颜色是必不可少的! 我覆盖了Jenkins用户的profile.ps1中的本地Write-Host。现在我所有的本地脚本都在詹金斯上着色
function Write-Host {
<#
.SYNOPSIS
Wrapper for Write-Host, adds ANSI SGR codes based on -foregroundColor and -backgroundColor
.DESCRIPTION
Renplace le Write-host standard, pour une compatibilité avec les sorties linux (JENKINS)
.PARAMETER object
Liste de String qui seront regroupe et affichée en couleur
.PARAMETER foregroundColor
Couleur du texte
.PARAMETER backgroundColor
Couleur de fond
.PARAMETER nonewline
si pas de retour a la ligne
.NOTES
Alban LOPEZ 2018
alban.lopez@gmail.com
http://git/PowerTech/
#>
param(
$object,
[ConsoleColor]$foregroundColor,
[ConsoleColor]$backgroundColor,
[switch]$nonewline
)
if (!(Get-Command Write-HostOriginal -ea 0).name){ # doit etre embarque (pour les scriptblock)
$global:ConsoleOutput = ''
$metaData = New-Object System.Management.Automation.CommandMetaData (Get-Command 'Microsoft.PowerShell.Utility\Write-Host')
Invoke-Expression "function Global:Write-HostOriginal { $([System.Management.Automation.ProxyCommand]::create($metaData)) }"
}
# https://msdn.microsoft.com/en-us/library/system.consolecolor(v=vs.110).aspx
# Converted to closest ANSI SGR equivalent
$AnsiColor = [pscustomobject][ordered]@{ # doit etre embarque (pour les scriptblock)
ForeGround = [pscustomobject][ordered]@{
Black = 30
Red = 91
DarkRed = 31
Green = 92
DarkGreen = 32
Yellow = 93
DarkYellow = 33
Blue = 94
DarkBlue = 34
Magenta = 95
DarkMagenta = 35
Cyan = 96
DarkCyan = 36
White = 97
Gray = 37
DarkGray = 90
}
BackGround = [pscustomobject][ordered]@{
Black = 40
White = 107
Red = 101
DarkRed = 41
Green = 102
DarkGreen = 42
Yellow = 103
DarkYellow = 43
Blue = 104
DarkBlue = 44
Magenta = 105
DarkMagenta = 45
Cyan = 106
DarkCyan = 46
Gray = 47
DarkGray = 100
}
style = [pscustomobject][ordered]@{
RESET = 0
BOLD_ON = 1
ITALIC_ON = 3
UNDERLINE_ON = 4
BLINK_ON = 5
REVERSE_ON = 7
# BOLD_OFF = 22
# ITALIC_OFF = 23
# UDERLINE_OFF = 24
# BLINK_OFF = 25
# REVERSE_OFF = 27
}
}
function Colorize-Text {
<#
.SYNOPSIS
Adds ANSI SGR codes to a string.
.DESCRIPTION
Adds ANSI SGR codes to a string.
.PARAMETER text
Text to be transformed.
.PARAMETER ansiSgrCode
ANSI SGR number to insert.
See https://en.wikipedia.org/wiki/ANSI_escape_code for details
Or use the [AnsiColor] enum.
Also accepts an array of SGR numbers, and will apply all of them.
.NOTES
Designed to play nicely with https://wiki.jenkins-ci.org/display/JENKINS/AnsiColor+Plugin
.EXAMPLE
Colorize-Text 'toto' 7,93,101
#>
param(
$object,
[int[]]$ansiCodes #https://en.wikipedia.org/wiki/ANSI_escape_code#graphics
)
return "$([char]27)[$($ansiCodes -join(';'))m$object$([char]27)[0m"
}
$ansiCodes = @()
if($style){
$ansiCodes += $AnsiColor.style.$style
}
if($foregroundColor){
$ansiCodes += $AnsiColor.ForeGround.$foregroundColor
}
if($backgroundColor) {
$ansiCodes += $AnsiColor.BackGround.$backgroundColor
}
# Write-HostOriginal (Colorize-Text $object -ansiCodes $ansiCodes ) -nonewline # | Out-Host
# (Colorize-Text $object -ansiCodes $ansiCodes ) | Out-Host
# [Console]::Write( (Colorize-Text $object -ansiCodes $ansiCodes ) )
if($foregroundColor -and $backgroundColor){
# Write-HostOriginal (Colorize-Text $object -ansiCodes $ansiCodes ) -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor -nonewline # | Out-Host
$global:ConsoleOutput += (Colorize-Text $object -ansiCodes $ansiCodes )
} elseif($foregroundColor){
# Write-HostOriginal (Colorize-Text $object -ansiCodes $ansiCodes ) -ForegroundColor $foregroundColor -nonewline # | Out-Host
$global:ConsoleOutput += (Colorize-Text $object -ansiCodes $ansiCodes )
} elseif($backgroundColor){
# Write-HostOriginal (Colorize-Text $object -ansiCodes $ansiCodes ) -BackgroundColor $backgroundColor -nonewline # | Out-Host
$global:ConsoleOutput += (Colorize-Text $object -ansiCodes $ansiCodes )
} else {
# Write-HostOriginal $object -nonewline # | Out-Host
$global:ConsoleOutput += $object
}
if (!$nonewline) {
Write-HostOriginal $global:ConsoleOutput # -NoNewline
$global:ConsoleOutput = ''
# Write-HostOriginal '$!' -fore magenta
}
}
此代码在线程和远程使用(Start-Job,Invoke-command)中与ScriptBlock兼容
答案 1 :(得分:2)
我以前从未使用它,所以我想我会尝试一下。基本上你只是将一个转义字符(ASCII 27)后跟一个左括号[
,然后将代码as described on this page直接放入字符串中。
为了使这更容易,我写了一个格式化字符串的函数:
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
请记住,如果您不再需要它,您必须关闭序列(我的意思是将样式和颜色设置回原来的样式)。
答案 2 :(得分:0)