我没有在任何使用PowerShell的机器上设置默认颜色,而是用代码创建了一个配置文件来进行颜色设置,这样我就可以从一个机器复制到另一个机器。设置控制台颜色可以很好地工作,但是,设置PSReadline颜色会将控制台的白色改为黑色。代码如下:
$consoleColorRaw = [int]0
$consoleColorError = [int]1
$consoleColorWarning = [int]2
$consoleColorDebug = [int]3
$consoleColorVerbose = [int]4
$consoleColorProgress = [int]5
function checkIfEnumColor($localColor)
{
switch($localColor.ToLower())
{
"black" {Break}
"darkBlue" {Break}
"darkGreen" {Break}
"darkCyan" {Break}
"darkRed" {Break}
"darkMagenta" {Break}
"darkYellow" {Break}
"gray" {Break}
"darkGray" {Break}
"blue" {Break}
"green" {Break}
"cyan" {Break}
"red" {Break}
"magenta" {Break}
"yellow" {Break}
"white" {Break}
default { return $false }
}
return $true
}
function Color-Console($localWhich, $fg, $bg)
{
$doClearHost = $false
$localValue = checkIfEnumColor $bg
if ($localValue -eq $false)
{
Write-Host ("Attempting to set background to non-enum value({0})." -f $bg)
$doClearHost = $false;
}
else
{
$localValue = checkIfEnumColor $fg
if ($localValue -eq $false)
{
Write-Output ("Attempting to set foreground to non-enum value({0})." -f $fg)
$doClearHost = $false;
}
else
{
switch($localWhich)
{
$consoleColorRaw { $Host.ui.rawui.foregroundcolor = $fg; $Host.ui.rawui.backgroundcolor = $bg; Break }
$consoleColorError { $Host.PrivateData.ErrorForegroundColor = $fg; $Host.PrivateData.ErrorBackgroundColor = $bg; Break }
$consoleColorWarning { $Host.PrivateData.WarningForegroundColor = $fg; $Host.PrivateData.WarningBackgroundColor = $bg; Break }
$consoleColorDebug { $Host.PrivateData.DebugForegroundColor = $fg; $Host.PrivateData.DebugBackgroundColor = $bg; Break }
$consoleColorVerbose { $Host.PrivateData.VerboseForegroundColor = $fg; $Host.PrivateData.VerboseBackgroundColor = $bg; Break }
$consoleColorProgress { $Host.PrivateData.ProgressForegroundColor = $fg; $Host.PrivateData.ProgressBackgroundColor = $bg; Break }
default { $doClearHost = $false; Break }
}
}
}
if ($doClearHost -eq $true)
{
Clear-Host
}
}
# I used the documentation in https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
# to figure out the numbers for the ansi codes... The dark colors use the 30's and 40's
# and the regular colors use the 90's and 100's (bright colors)
function setPSReadlineColor($localWhich, $foreG, $backG)
{
$fg = 0;
$bg = 0;
switch($foreG.ToLower())
{
"black" {$fg = 30;Break}
"darkRed" {$fg = 31;Break}
"darkGreen" {$fg = 32;Break}
"darkYellow" {$fg = 33;Break}
"darkBlue" {$fg = 34;Break}
"darkMagenta" {$fg = 35;Break}
"darkCyan" {$fg = 36;Break}
"darkGray" {$fg = 37;Break}
"gray" {$fg = 90;Break}
"red" {$fg = 91;Break}
"green" {$fg = 92;Break}
"yellow" {$fg = 93;Break}
"blue" {$fg = 94;Break}
"magenta" {$fg = 95;Break}
"cyan" {$fg = 96;Break}
"white" {$fg = 97;Break}
default {Write-Host ("Attempting to set '{0}' foreground color to non-enum value({1})." -f $localWhich, $foreG); return }
}
switch($backG.ToLower())
{
"black" {$bg = 40;Break}
"darkRed" {$bg = 41;Break}
"darkGreen" {$bg = 42;Break}
"darkYellow" {$bg = 43;Break}
"darkBlue" {$bg = 44;Break}
"darkMagenta" {$bg = 45;Break}
"darkCyan" {$bg = 46;Break}
"darkGray" {$bg = 47;Break}
"gray" {$bg = 100;Break}
"red" {$bg = 101;Break}
"green" {$bg = 102;Break}
"yellow" {$bg = 103;Break}
"blue" {$bg = 104;Break}
"magenta" {$bg = 105;Break}
"cyan" {$bg = 106;Break}
"white" {$bg = 107;Break}
default {Write-Host ("Attempting to set '{0}' background color to non-enum value({1})." -f $localWhich, $backG);return }
}
$newColor = "$([char]0x1b)[{0};{1}m" -f $fg, $bg
Set-PSReadLineOption -Colors @{$localWhich = $newColor}
}
Color-Console $consoleColorRaw "DarkMagenta" "Gray"
#Color-Console $consoleColorError "Red" "Gray"
Color-Console $consoleColorError "Yellow" "DarkCyan"
Color-Console $consoleColorWarning "Magenta" "Gray"
Color-Console $consoleColorDebug "Yellow" "Gray"
Color-Console $consoleColorVerbose "Green" "Gray"
Color-Console $consoleColorProgress "Cyan" "Gray"
setPSReadlineColor "Command" "Magenta" "White"
setPSReadlineColor "Variable" "Green" "Gray"希望有人能发现我做错了什么:)谢谢!
发布于 2021-05-19 01:49:12
我发现powershell 7(至少与5.1相比)将其配置文件保存到不同的位置,因此请仔细检查:
# PS5
$profile
C:\users\username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
# PS7
$profile
C:\users\username\Documents\PowerShell\Microsoft.PowerShell_profile.ps1我发现如果你设置了一个无效的ansi转义序列,Set-PSReadLineOption会将你的文本颜色设置为默认值。但是不同的版本有不同的行为:
# PS5: No error, but sets color to terminal default (white in my case)
Set-PSReadLineOption -Colors @{Command='Magenta,Yellow'}
# PS7: Errors out
Set-PSReadLineOption -Colors @{Command='Magenta,Yellow'}
[Error] Set-PSReadLineOption: 'Magenta,Yellow' is not a valid color value. It must be a ConsoleColor, ANSI escape sequence, or RGB value with optional leading '#'.我把这两个建议放在第一位,因为你问题中的脚本按原样粘贴到我的个人资料中,我在powershell 5.1和7.1中都能工作。
您使用的是哪个航站楼?内置终端/cmd、PS Core、ISE、VS Code还是新的Windows终端?它们在这方面的表现基本相同,但可能有助于解释您所看到的内容。
https://stackoverflow.com/questions/67581901
复制相似问题