简体   繁体   English

Powershell-Function Write-Host output 不会打印黑色

[英]Powershell-Function Write-Host output won't print with color Black

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.我已经从 jdhitsolutions 中获取了 Add-Border function并一直在搞乱它以向 output 添加颜色,而不是像在一些示例中显示的 Z78E6221F6393D1356681DB398ZF 中所示的管道一样。

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.如果我正常使用 write-host 它可以工作,所以任何系统 colors 都没有问题,但如果我尝试使用 function 它只是正常的文本着色。

below is my tweaked function.下面是我调整的 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
    }

    }
    
}

[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 . [ConsoleColor]是一个枚举,与第一个成员Black关联的值恰好是0 - 因此,当您的if语句评估$ForegroundColor时,它看到的只是 0,这被解释为$false

Change the if statements to test whether any value was actually bound to either parameter variable:更改if语句以测试是否有任何值实际绑定到任一参数变量:

$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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM