在Write-Host中,您可以将前景颜色设置为
Write-Host "test" -ForegroundColor Green你会使用十六进制代码吗?喜欢
Write-Host "test" -ForegroundColor FFFFFF如果我希望前景色是未列出的颜色
[System.Enum]::GetValues([System.ConsoleColor])我做什么好?
发布于 2020-06-28 10:29:42
我想你可以使用像psreadline这样的转义代码。运行"get-psreadlineoptions“来查看其中的一些。该命令的文档具有指向代码的链接。https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_在Powershell7中,您可以使用“e”进行转义。
write-host "$([char]0x1b)[91mhi"
hi # appears red
Command "$([char]0x1b)[31m" red # 93 bright yellow
Comment "$([char]0x1b)[32m" green
ContinuationPrompt "$([char]0x1b)[37m" white # 33 yellow
DefaultToken "$([char]0x1b)[37m" white
Emphasis "$([char]0x1b)[96m" bright cyan
Error "$([char]0x1b)[91m" bright red
Keyword "$([char]0x1b)[92m" bright green
Member "$([char]0x1b)[97m" bright white
Number "$([char]0x1b)[97m" bright white
Operator "$([char]0x1b)[90m" bright black
Parameter "$([char]0x1b)[90m" bright black
Selection "$([char]0x1b)[30;47m" black on white # 35;43 magenta;yellow
String "$([char]0x1b)[36m" cyan
Type "$([char]0x1b)[37m" white
Variable "$([char]0x1b)[92m" bright green有一个叫做Pansies的模块可以做到这一点。它会安装一个新的写主机。它也支持xterm颜色,DodgerBlue等...
发布于 2020-06-28 22:44:52
另一种选择是创建十六进制代码和控制台颜色名称的哈希表映射,并使用:
$colorMap = [ordered]@{
'000000' = 'Black'
'140E88' = 'DarkBlue'
'00640A' = 'DarkGreen'
'008B87' = 'DarkCyan'
'8B0000' = 'DarkRed'
'820087' = 'DarkMagenta'
'AAAA00' = 'DarkYellow'
'A9A9A9' = 'Gray'
'808080' = 'DarkGray'
'0000FF' = 'Blue'
'00FF00' = 'Green'
'00FFFF' = 'Cyan'
'FF0000' = 'Red'
'FF00FF' = 'Magenta'
'FFFF00' = 'Yellow'
'FFFFFF' = 'White'
}
foreach($colorCode in $colorMap.Keys) {
Write-Host "Testing color $colorCode" -ForegroundColor $colorMap[$colorCode]
}当然,使用这种方法只能使用$colorMap包含的十六进制代码作为键,否则会抛出异常
https://stackoverflow.com/questions/62617181
复制相似问题