简体   繁体   English

包含 PowerShell 中文本颜色的变量

[英]Variables that contains the color of a text in PowerShell

Is it possible to store in a variable the color of a text?是否可以将文本的颜色存储在变量中? I tried this but it doesn't work:我试过这个但它不起作用:

$format1 = "-ForegroundColor White"
$format2 = "-BackgroundColor Black"

Write-Host "Example" $format1 $format2

It returns:它返回:

"Example -ForegroundColor White -BackgroundColor Black" #(not colored)

Here is how you can accomplish what you're trying to achieve.以下是您如何完成您想要实现的目标。

$format1 = @{
    ForegroundColor="White"
    BackgroundColor="Black"
}

Write-Host "Example" @format1

This method is called Splatting and basically is how you can pass multiple parameters to a function using a hashtable .这种方法称为Splatting ,基本上就是如何使用hashtable将多个参数传递给 function 。

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

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