在一个命令Powershell多个前景色 [英] Muliple Foreground Colors in Powershell in One Command

查看:174
本文介绍了在一个命令Powershell多个前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个语句输出许多不同的前景颜色。

  PS C:\>写主机Red-ForegroundColor Red 
Red

此输出为红色。 p>

  PS C:\>写主机Blue-ForegroundColor Blue 
Blue

此输出为蓝色。 p>

  PS C:\>写主机红色,蓝色-ForegroundColor红色,蓝色
红色蓝色

此输出为品红色,但我想通过one命令将颜色设置为红色为红色,蓝色为蓝色。我如何做到这一点?

解决方案

Josh方法是如此之大,我实际上去扩展它有点为我的需要。我写了博客文章



它实际上在Josh脚本中带来了额外的检查和功能。


I want to output many different foreground colors with one statement.

PS C:\> Write-Host "Red" -ForegroundColor Red
Red

This output is red.

PS C:\> Write-Host "Blue" -ForegroundColor Blue
Blue

This output is blue.

PS C:\> Write-Host "Red", "Blue" -ForegroundColor Red, Blue
Red Blue

This output is magenta, but I want the color to be Red for the word red, and blue for the word blue via the one command. How can I do that?

解决方案

Josh method was so great that I actually went and expanded it a bit for my needs. I've written blog post How to format PowerShell with Multiple Colors about it (with screenshots and all - for the whole story and usage).

function Write-Color([String[]]$Text, [ConsoleColor[]]$Color = "White", [int]$StartTab = 0, [int] $LinesBefore = 0,[int] $LinesAfter = 0) {
$DefaultColor = $Color[0]
if ($LinesBefore -ne 0) {  for ($i = 0; $i -lt $LinesBefore; $i++) { Write-Host "`n" -NoNewline } } # Add empty line before
if ($StartTab -ne 0) {  for ($i = 0; $i -lt $StartTab; $i++) { Write-Host "`t" -NoNewLine } }  # Add TABS before text
if ($Color.Count -ge $Text.Count) {
    for ($i = 0; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine } 
} else {
    for ($i = 0; $i -lt $Color.Length ; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine }
    for ($i = $Color.Length; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $DefaultColor -NoNewLine }
}
Write-Host
if ($LinesAfter -ne 0) {  for ($i = 0; $i -lt $LinesAfter; $i++) { Write-Host "`n" } }  # Add empty line after
}

Write-Color -Text "Red ", "Green ", "Yellow " -Color Red,Green,Yellow

Write-Color -Text "This is text in Green ",
               "followed by red ",
               "and then we have Magenta... ",
               "isn't it fun? ",
               "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan


 Write-Color -Text "This is text in Green ",
               "followed by red ",
               "and then we have Magenta... ",
               "isn't it fun? ",
               "Here goes DarkCyan" -Color Green,Red,Magenta,White,DarkCyan -StartTab 3 -LinesBefore 1 -LinesAfter 1

Write-Color "1. ", "Option 1" -Color Yellow, Green
Write-Color "2. ", "Option 2" -Color Yellow, Green
Write-Color "3. ", "Option 3" -Color Yellow, Green
Write-Color "4. ", "Option 4" -Color Yellow, Green
Write-Color "9. ", "Press 9 to exit" -Color Yellow, Gray -LinesBefore 1

It actually brings additional checks and features over Josh script.

这篇关于在一个命令Powershell多个前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆