forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Out-ConditionalColor.ps1
112 lines (95 loc) · 3.06 KB
/
Out-ConditionalColor.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
Function Out-ConditionalColor {
[cmdletbinding(DefaultParameterSetName="property")]
Param(
[Parameter(Position=0,Mandatory,
HelpMessage="Enter an hashtable of conditional properties and colors.",
ParameterSetName="property")]
[ValidateScript({
#validate colors
$allowed = [enum]::GetNames([system.consolecolor])
$bad = $_.Values | Where-Object {$allowed -notcontains $_}
if ($bad) {
$valid = $allowed -join ','
Throw "You are using one or more invalid colors: $($bad -join ','). Valid colors are $Valid"
}
else {
$True
}
})]
[hashtable]$PropertyConditions,
[Parameter(Mandatory,HelpMessage="Enter a property name.",
ParameterSetName="property")]
[string]$Property,
[Parameter(Position=0,Mandatory,
HelpMessage="Enter an ordered hashtable of conditions and colors.",
ParameterSetName="conditions")]
[System.Collections.Specialized.OrderedDictionary]$Conditions,
[Parameter(Mandatory,ValueFromPipeline)]
[PSObject[]]$InputObject
)
Begin {
Write-Debug "Starting $($MyInvocation.MyCommand)"
write-Debug "Using parameter set $($pscmdlet.ParameterSetName)"
#save original color
$saved = $Host.UI.RawUI.ForegroundColor
Write-Debug "Original foreground color is $saved"
if ($PSCmdlet.ParameterSetName -eq 'conditions') {
#we'll need this later in the Process script block
#if doing complex processing
Write-Debug "Getting hash table enumerator and names"
$compare = $conditions.GetEnumerator().name
Write-Debug $($compare | out-string)
#build an If/ElseIf construct on the fly
#the Here strings must be left justified
$If=@"
if ($($compare[0])) {
`$host.ui.RawUI.ForegroundColor = '$($conditions.item($($compare[0])))'
}
"@
#now add the remaining comparisons as ElseIf
for ($i=1;$i -lt $conditions.count;$i++) {
$If+="elseif ($($compare[$i])) {
`$host.ui.RawUI.ForegroundColor = '$($conditions.item($($compare[$i])))'
}
"
} #for
#add the final else
$if+=@"
Else {
`$host.ui.RawUI.ForegroundColor = `$saved
}
"@
Write-Debug "Complex comparison:"
Write-Debug $If
} #if complex parameter set
} #Begin
Process {
If ($PSCmdlet.ParameterSetName -eq 'conditions') {
#Use complex processing
Invoke-Expression $if
} #end complex
else {
#get property value as a string
$value = $Inputobject.$Property.ToString()
Write-Debug "Testing property value $value"
if ($PropertyConditions.containsKey($value)) {
Write-Debug "Property match"
$host.ui.RawUI.ForegroundColor= $PropertyConditions.item($value)
}
else {
#use orginal color
Write-Debug "No matches found"
$host.ui.RawUI.ForegroundColor= $saved
}
} #simple
#write the object to the pipeline
Write-Debug "Write the object to the pipeline"
$_
} #Process
End {
Write-Debug "Restoring original foreground color"
#set color back
$host.ui.RawUI.ForegroundColor = $saved
} #end
} #close function