As the title says, how do we display console output from PowerShell scripts in color? We're using v16.12 of GoCD.
Edit: Due to company policy we're not allowed to use write-host. Please don't ask why :)
As the title says, how do we display console output from PowerShell scripts in color? We're using v16.12 of GoCD.
Edit: Due to company policy we're not allowed to use write-host. Please don't ask why :)
The answer is absolutely not related to GoCD. What about
[System.Console]::BackgroundColor="blue"
[System.Console]::ForegroundColor="yellow"
[System.Console]::WriteLine("yellow on blue");
If you want a potentially easier way to handle those colors, you could install, and use the TMOutput module, a module I've written. It uses System.Console; however, it does so with less coding on your part.
Here's a link to a post about it: http://tommymaynard.com/script-sharing-write-output-gets-foreground-and-background-colors-and-more-2016/. On that page, is a link to the module on Microsoft's PowerShell Gallery. If you use PowerShellGet, you can download and install it using the below command.
Install-Module -Name TMOutput
Edit: Another thing the module is going to do for you, is reset the colors back to what they were before you put what you wanted in color. You'd have to do that yourself if you just use [System.Console] on your own.
This is only arbitrarily not Write-Host
, but it is another option that might satisfy the requirements.
$Host.UI.Write('Cyan','DarkMagenta','Text to display')
Overloads:
void Write(string value)
void Write(System.ConsoleColor foregroundColor, System.ConsoleColor backgroundColor, string value)
Also while not directly related, it's worth noting that if your requirements are actually to satisfy PSScriptAnalyzer rules you can wrap any Write-Host
commands in a function that uses the verb Show
(e.g. Show-GoCDOutput
) and PSScriptAnalyzer will skip it.