Vop*_*pel 8 powershell colors verbose powershell-core
我希望以下代码能够使用默认前景色打印详细文本:
$Host.PrivateData.VerboseForegroundColor = [console]::ForegroundColor
Write-Verbose 'Test' -Verbose
Run Code Online (Sandbox Code Playgroud)
但是,它照常打印黄色文本。更改错误前景色确实有效:
$Host.PrivateData.ErrorForegroundColor = [console]::ForegroundColor
Write-Error 'test'
Run Code Online (Sandbox Code Playgroud)
我发现规避这个问题的唯一方法是这样做:
Write-Verbose 'Test' -Verbose *>&1 | Write-Host
Run Code Online (Sandbox Code Playgroud)
但这并没有真正改变详细颜色,它只是强制它使用 Write-Host 作为默认文本直接打印到控制台主机。我确实知道 Write-Host 确实可以让您将消息颜色更改为您想要的任何颜色,但这并不是一个理想的解决方案。
nin*_*key 10
在 中Powershell 7.2+,$Host.PrivateData这不是设置样式的正确方法。它的存在是为了向后兼容。详细信息请参见全新about_*页面:about_ANSI_Terminals
你要
\n\n$PSStyle.Formatting.Verbose = $PSStyle.Foreground.FromRgb(0x34f2aa)\nRun Code Online (Sandbox Code Playgroud)\n查看文档,它添加了一堆选项:粗体,闪烁,隐藏,反向,斜体,下划线,文件信息...... about_ANSI_Terminals
\nfunction testVerbose { \n [CmdletBinding()]\n param( [Parameter() ]$x )\n "$x"\n $X.GetType().FullName | write-verbose\n \n}\n\ntestVerbose 34.5 -Verbose ; \n\n$PSStyle.Formatting.Verbose = $PSStyle.Foreground.FromRgb(0x34f2aa)\n\ntestVerbose 34.5 -Verbose\nRun Code Online (Sandbox Code Playgroud)\n查看ANSI控制字符的一种方法是替换“``e”。
# I saved the value before changing it\n$originalVerbose -replace "`e", \'\'\n\n$PSStyle.Formatting.Verbose -replace "`e", \'\'\nRun Code Online (Sandbox Code Playgroud)\n\n注意:已切换为24 位色ANSI 转义序列
\nFormat-ControlChar将所有控制字符转换为其Symbol版本,而不仅仅是e,从而使值可以安全地通过管道传输。
很简单,只需将 0x2400 添加到代码点即可。compart.com/block/U+2400
\n> "$($PSStyle.Background.BrightCyan)Power$($PSStyle.Underline)$($PSStyle.Bold)Shell$($PSStyle.Reset)"\n | Format-ControlChar\n\n\xe2\x90\x9b[106mPower\xe2\x90\x9b[4m\xe2\x90\x9b[1mShell\xe2\x90\x9b[0m\nRun Code Online (Sandbox Code Playgroud)\n