1+ function WriteMessage
2+ {
3+ [CmdletBinding ()]
4+ Param
5+ (
6+ # The Type of message you'd like, its just a prefix to the message.
7+ [Parameter (Mandatory = $true , Position = 0 )]
8+ [string ]
9+ $Type ,
10+
11+ # The message to display
12+ [Parameter (Mandatory = $true , Position = 1 )]
13+ [string ]
14+ $Message ,
15+
16+ # Color of the message, it will default to the hosts' verbose color.
17+ [Parameter (Position = 2 )]
18+
19+ $ForegroundColor = $host.PrivateData.VerboseForegroundColor ,
20+ # Background color of the message, it will default to the hosts' verbose color.
21+ [Parameter (Position = 3 )]
22+ $BackgroundColor = $host.PrivateData.VerboseBackgroundColor
23+ )
24+
25+ if ($ForegroundColor -is [System.Windows.Media.Color ]){ $ForegroundColor = ConvertColor $ForegroundColor }
26+ if ($BackgroundColor -is [System.Windows.Media.Color ])
27+ {
28+ # if its a transparent color
29+ if ($BackgroundColor -eq " #00FFFFFF" ) {$BackgroundColor = $host.PrivateData.ConsolePaneBackgroundColor }
30+ $BackgroundColor = ConvertColor $BackgroundColor
31+ }
32+
33+ # todo check to see if the preference is on or off
34+
35+ Write-Host " $ ( $Type.ToUpper ()) : $Message " - ForegroundColor $ForegroundColor - BackgroundColor $BackgroundColor
36+ }
37+
38+
39+ function ConvertColor ($color )
40+ {
41+ [int ]$bright = if ($color.R -gt 128 -bor $color.G -gt 128 -bor $color.B -gt 128 ){8 }else {0 }
42+ [int ]$r = if ($color.R -gt 64 ){4 }else {0 }
43+ [int ]$g = if ($color.G -gt 64 ){2 }else {0 }
44+ [int ]$b = if ($color.B -gt 64 ){1 }else {0 }
45+ [consolecolor ]($bright -bor $r -bor $g -bor $b )
46+ }
0 commit comments