Home > Software design >  Powershell-Function Write-Host output won't print with color Black
Powershell-Function Write-Host output won't print with color Black

Time:09-21

Morning Folks,

I've taken the Add-Border function from jdhitsolutions and have been messing around with it to add coloring to the output instead of piping the output to Write-Host like he's shown in some examples.

Now I've gotten it to work with every color but Black. For some reason that color won't print. If I use write-host normally it works, so it's nothing wrong with any of the system colors, but if I try with the function it just normal text coloring.

below is my tweaked function.

Function Add-Border {
<#
    .Synopsis
    Create a text border around a string.
    .Description
    This command will create a character or text based border around a line of text. You might use this to create a formatted text report or to improve the display of information to the screen.
    .Parameter Text
    A single line of text that will be wrapped in a border.
    .Parameter Character
    The character to use for the border. It must be a single character.
    .Parameter InsertBlanks
    Insert blank lines before and after the text. The default behavior is to create a border box close to the text. See examples.
    .Parameter Tab
    Insert the specified number of tab characters before the result.
    .Example
    PS C:\> add-border "PowerShell Wins!"
    ********************
    * PowerShell Wins! *
    ********************
    .Example
    PS C:\> add-border "PowerShell Wins!" -tab 1
        ********************
        * PowerShell Wins! *
        ********************
        
    .Example
    PS C:\> add-border "PowerShell Wins!" -character "-" -insertBlanks
    --------------------
    -                  -
    - PowerShell Wins! -
    -                  -
    --------------------
#>
[CmdletBinding()]
Param(
    [Parameter(Position = 0, Mandatory,ValueFromPipeline)]
    [ValidateNotNullOrEmpty()]
    [string]$Text,

    [Parameter(Position=1, Mandatory)]
    [ValidateNotNullOrEmpty()]
    [validateScript({$_.length -eq 1})]
    [ValidatePattern("[ -~]")]
    [string]$Character,

    [Parameter()]
    [Switch]$InsertBlanks,
    
    [int]$Tab = 0,

    [Parameter()]
    [System.ConsoleColor]$ForegroundColor,

    [Parameter()]
    [System.ConsoleColor]$BackgroundColor,

)
    
    Process {
        # // Gets the length of this input Text
        $Length = $Text.Length
        
        # // Defines the amount of tabs
        $tabs = "`t"*$tab

        # // Defines the Border lines
        $line = $Character * ($Length 4)
    
    # // Adds in empty lines above and below the input Text
        if ($insertBlanks) {
            $body = @"
$tabs$character $((" ")*$Length) $character
$tabs$Character $text $Character
$tabs$character $((" ")*$Length) $character
"@
    }    
    else {
        $body = "$tabs$Character $text $Character"
    }

# // Defines a here string with the final result
$OutBorder = @"
$tabs$line
$body
$tabs$line
"@
    
# // Outputs the result
    $Colors = @{}
    If($ForegroundColor){
        $Colors.Add("ForegroundColor",$ForegroundColor)
    }

    If($BackgroundColor){
        $Colors.Add("BackgroundColor",$BackgroundColor)
    }

    If(-Not(($ForegroundColor)) -and (-Not($BackgroundColor))){
        $OutBorder
    }else{
        Write-Host $OutBorder @Colors
    }

    }
    
}

CodePudding user response:

[ConsoleColor] is an enum, and the value associated with the first member, Black, happens to be 0 - so when $ForegroundColor is evaluated by your if statement, all it sees is 0, which is interpreted as $false.

Change the if statements to test whether any value was actually bound to either parameter variable:

$Colors = @{}

# Always use `$PSBoundParameters` to check whether a parameter argument was passed or not
$foregroundColorBound = $PSBoundParameters.ContainsKey('ForegroundColor')
$backgroundColorBound = $PSBoundParameters.ContainsKey('BackgroundColor')

If($foregroundColorBound){
    $Colors.Add("ForegroundColor", $ForegroundColor)
}

If($backgroundColorBound){
    $Colors.Add("BackgroundColor", $BackgroundColor)
}

if( -not($foregroundColorBound) -and -not($backgroundColorBound) ){
    $OutBorder
}
else {
    Write-Host $OutBorder @Colors
}
  • Related