颜色Powershell中的字符串部分

时间:2014-12-29 13:18:05

标签: colors foreground

我正在尝试仅将字符串“None Provide”与整行进行着色。

到目前为止,我有:

$owned | % {write-host "Managing Group: " + ("None Found" -foreground red) "Group Description: None Provided"}

问题在于()中的-foreground,'f'灰色,导致错误。它的工作原理(通过着色整行)没有()。你能帮我上色“没找到”

通过使用Overflowed的答案,它帮助我解决了这个问题。谢谢!

编辑:

if ($owned -eq $Null){
$owned | % {write-host "Managing Group: " -nonewline}
$owned | % {write-host "None Found " -foreground red -nonewline}
$owned | % {write-host "Group Description: " -nonewline}
$owned | % {write-host "None Provided " -foreground red}
}

elseif ($description -eq $Null){
$owned | % {write-host "Managing Group: "($_.name) "Group Description: " -nonewline}
$owned | % {write-host "None Provided " -foreground red}
}

else {
$owned | % {write-host "Managing Group: "($_.name) "Group Description: " ($_.description -replace "`r`n", "  ")}
}

2 个答案:

答案 0 :(得分:4)

将多个write-host与-nonewline开关一起使用

write-host "Managing Group: " -nonewline
write-host "None Found" -foreground red -nonewline
write-host "Group Description: None Provided"

答案 1 :(得分:4)

使用便利功能补充overflowed's helpful answer

下面定义的函数允许您在颜色规范中嵌入颜色规范,用于子串的选择性着色; e.g:

Write-HostColored "#green#Green foreground.# Default colors. #blue:white#Blue on white."

的产率:

sample output

阅读以下功能的评论以获取详细信息 该功能很感激地改编自this blog post

高级函数Write-HostColored

源代码

function Write-HostColored() {
<#
.SYNOPSIS
A wrapper around Write-Host that supports selective coloring of
substrings.

.DESCRIPTION
In addition to accepting a default foreground and background color,
you can embed one or more color specifications in the string to write, 
using the following syntax:
#<fgcolor>[:<bgcolor>]#<text>#

<fgcolor> and <bgcolor> must be valid [ConsoleColor] values, such as 'green' or 'white' (case does not matter).
Everything following the color specification up to the next '#' or, impliclitly, the end of the string
is written in that color.

Note that nesting of color specifications is not supported.
As a corollary, any token that immediately follows a color specification is treated
as text to write, even if it happens to be a technically valid color spec too.
This allows you to use, e.g., 'The next word is #green#green#.', without fear
of having the second '#green' be interpreted as a color specification as well.

.PARAMETER ForegroundColor
Specifies the default text color for all text portions
for which no embedded foreground color is specified.

.PARAMETER BackgroundColor
Specifies the default background color for all text portions
for which no embedded background color is specified.

.PARAMETER NoNewline
Output the specified string withpout a trailing newline.

.NOTES
While this function is convenient, it will be slow with many embedded colors, because,
behind the scenes, Write-Host must be called for every colored span.

.EXAMPLE
Write-HostColored "#green#Green foreground.# Default colors. #blue:white#Blue on white."

.EXAMPLE
'#black#Black on white (by default).#Blue# Blue on white.' | Write-HostColored -BackgroundColor White

#>
    [CmdletBinding(ConfirmImpact='None', SupportsShouldProcess=$false, SupportsTransactions=$false)]
    param(
        [parameter(Position=0, ValueFromPipeline=$true)]
        [string[]] $Text
        ,
        [switch] $NoNewline
        ,
        [ConsoleColor] $BackgroundColor =  $host.UI.RawUI.BackgroundColor
        ,
        [ConsoleColor] $ForegroundColor = $host.UI.RawUI.ForegroundColor
    )

    begin {
        # If text was given as an operand, it'll be an array.
        # Like Write-Host, we flatten the array into a single string
        # using simple string interpolation (which defaults to separating elements with a space,
        # which can be changed by setting $OFS).
        if ($Text -ne $null) {
            $Text = "$Text"
        }
    }

    process {
        if ($Text) {

            # Start with the foreground and background color specified via
            # -ForegroundColor / -BackgroundColor, or the current defaults.
            $curFgColor = $ForegroundColor
            $curBgColor = $BackgroundColor

            # Split message into tokens by '#'.
            # A token between to '#' instances is either the name of a color or text to write (in the color set by the previous token).
            $tokens = $Text.split("#")

            # Iterate over tokens.            
            $prevWasColorSpec = $false
            foreach($token in $tokens) {

                if (-not $prevWasColorSpec -and $token -match '^([a-z]+)(:([a-z]+))?$') { # a potential color spec.
                    # If a token is a color spec, set the color for the next token to write.
                    # Color spec can be a foreground color only (e.g., 'green'), or a foreground-background color pair (e.g., 'green:white')
                    try {
                        $curFgColor = [ConsoleColor]  $matches[1]
                        $prevWasColorSpec = $true
                    } catch {}
                    if ($matches[3]) {
                        try {
                            $curBgColor = [ConsoleColor]  $matches[3]
                            $prevWasColorSpec = $true
                        } catch {}
                    }
                    if ($prevWasColorSpec) {
                        continue                    
                    }
                }

                $prevWasColorSpec = $false

                if ($token) {
                    # A text token: write with (with no trailing line break).
                    # !! In the ISE - as opposed to a regular PowerShell console window,
                    # !! $host.UI.RawUI.ForegroundColor and $host.UI.RawUI.ForegroundColor inexcplicably 
                    # !! report value -1, which causes an error when passed to Write-Host.
                    # !! Thus, we only specify the -ForegroundColor and -BackgroundColor parameters
                    # !! for values other than -1.
                    $argsHash = @{}
                    if ([int] $curFgColor -ne -1) { $argsHash += @{ 'ForegroundColor' = $curFgColor } }
                    if ([int] $curBgColor -ne -1) { $argsHash += @{ 'BackgroundColor' = $curBgColor } }
                    Write-Host -NoNewline @argsHash $token
                }

                # Revert to default colors.
                $curFgColor = $ForegroundColor
                $curBgColor = $BackgroundColor

            }
        }
        # Terminate with a newline, unless suppressed
        if (-not $NoNewLine) { write-host }
    }
}