在一个命令中使用 PowerShell 中的多种前景色 [英] Multiple foreground colors in PowerShell in one command

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

问题描述

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

PS C:>写入主机红色"-ForegroundColor 红色红色的

这个输出是红色的.

PS C:>写入主机蓝色"-ForegroundColor 蓝色蓝色

这个输出是蓝色的.

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

此输出是洋红色,但我希望通过 one 命令将单词 red 的颜色设为红色,将单词 blue 设为蓝色.我该怎么做?

解决方案

编辑(2018 年 5 月 7 日):我已将 Write-Color 更新为 0.5 并发布它作为模块.代码现已发布在 github 上.

0.5 中的变化:

  • 添加背景色
  • 为较短的代码添加了别名 T/B/C
  • 为函数添加别名(可与WC"一起使用)
  • 修复了模块发布

0.4 的变化

  • 修复了一些小问题
  • 作为模块发布

资源链接:

  • 它实际上为 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?

    解决方案

    Edit (7th May 2018): I've updated Write-Color to 0.5 and published it as module. Also code is now published on github.

    Changes in 0.5:

    • added backgroundcolor
    • added aliases T/B/C to shorter code
    • added alias to function (can be used with "WC")
    • fixes to module publishing

    Changes in 0.4

    • fixed small issues
    • published as module

    Links to resources:

    Thanks to published module you can easily use the code as below:

    Install-Module PSWriteColor
    Write-Color -Text "Some","Text" -Color Yellow,Red
    

    There is no more need to copy/paste code. Enjoy.

    Old code is below. It's highly advised to use links above for newest code:

    Edit (9th April 2018): I've updated Write-Color to v0.3. Feel free to get it at my site where I'm maintaining Write-Color. There are few small changes. Inluded -NoNewLine and -ShowTime option.

    Edit (Jun 2017): updated with new version, added logging to file for logging purposes

    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, [string] $LogFile = "", $TimeFormat = "yyyy-MM-dd HH:mm:ss") {
        # version 0.2
        # - added logging to file
        # version 0.1
        # - first draft
        # 
        # Notes:
        # - TimeFormat https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
    
        $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
        if ($LogFile -ne "") {
            $TextToFile = ""
            for ($i = 0; $i -lt $Text.Length; $i++) {
                $TextToFile += $Text[$i]
            }
            Write-Output "[$([datetime]::Now.ToString($TimeFormat))]$TextToFile" | Out-File $LogFile -Encoding unicode -Append
        }
    }
    
    
    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
    
    
    
    Write-Color -LinesBefore 2 -Text "This little ","message is ", "written to log ", "file as well." -Color Yellow, White, Green, Red, Red -LogFile "C:	esting.txt" -TimeFormat "yyyy-MM-dd HH:mm:ss"
    Write-Color -Text "This can get ","handy if ", "want to display things, and log actions to file ", "at the same time." -Color Yellow, White, Green, Red, Red -LogFile "C:	esting.txt"
    

    It actually brings additional checks and features over Josh script.

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

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