简体   繁体   English

PowerShell 获取写入主机的默认前景色

[英]PowerShell get default foreground color for write-host

I have a function to write an output log to file and to console.我有一个 function 将 output 日志写入文件和控制台。 The Write-Host part I'd like to color up a bit;-) Write-Host 部分我想稍微着色一下;-)

switch($logLevel)
{
   "FATAL" { $ConsoleWriteColor = "blue"; break }
   "ERROR" { $ConsoleWriteColor = "red"; break }
   "WARN" { $ConsoleWriteColor = "yellow"; break }
   default { $ConsoleWriteColor = "white"; break }

}
Write-Host -ForegroundColor $ConsoleWriteColor "Hello world"

So my question is for the default case: How can I get the current foreground color?所以我的问题是针对默认情况:如何获得当前的前景色? I guess it will not always be white?!我猜它不会永远是白色的?!

Current foreground and background can be obtained like this. 可以像这样获得当前的前景和背景。

PS>(get-host).ui.rawui.ForegroundColor
Gray
PS>(get-host).ui.rawui.BackgroundColor
Black

This one worked for me both in ISE and the native shell:这个在 ISE 和本机 shell 中都对我有用:

[System.Console]::ForegroundColor

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

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