-
Notifications
You must be signed in to change notification settings - Fork 6
/
Set-CellColor.ps1
171 lines (152 loc) · 7.71 KB
/
Set-CellColor.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Function Set-CellColor
{ <#
.SYNOPSIS
Function that allows you to set individual cell colors in an HTML table
.DESCRIPTION
To be used inconjunction with ConvertTo-HTML this simple function allows you
to set particular colors for cells in an HTML table. You provide the criteria
the script uses to make the determination if a cell should be a particular
color (property -gt 5, property -like "*Apple*", etc).
You can add the function to your scripts, dot source it to load into your current
PowerShell session or add it to your $Profile so it is always available.
To dot source:
.".\Set-CellColor.ps1"
.PARAMETER Property
Property, or column that you will be keying on.
.PARAMETER Color
Name or 6-digit hex value of the color you want the cell to be
.PARAMETER InputObject
HTML you want the script to process. This can be entered directly into the
parameter or piped to the function.
.PARAMETER Filter
Specifies a query to determine if a cell should have its color changed. $true
results will make the color change while $false result will return nothing.
Syntax
<Property Name> <Operator> <Value>
<Property Name>::= the same as $Property. This must match exactly
<Operator>::= "-eq" | "-le" | "-ge" | "-ne" | "-lt" | "-gt"| "-approx" | "-like" | "-notlike"
<JoinOperator> ::= "-and" | "-or"
<NotOperator> ::= "-not"
The script first attempts to convert the cell to a number, and if it fails it will
cast it as a string. So 40 will be a number and you can use -lt, -gt, etc. But 40%
would be cast as a string so you could only use -eq, -ne, -like, etc.
.PARAMETER Row
Instructs the script to change the entire row to the specified color instead of the individual cell.
.INPUTS
HTML with table
.OUTPUTS
HTML
.EXAMPLE
get-process | convertto-html | set-cellcolor -Propety cpu -Color red -Filter "cpu -gt 1000" | out-file c:\test\get-process.html
Assuming Set-CellColor has been dot sourced, run Get-Process and convert to HTML.
Then change the CPU cell to red only if the CPU field is greater than 1000.
.EXAMPLE
get-process | convertto-html | set-cellcolor cpu red -filter "cpu -gt 1000 -and cpu -lt 2000" | out-file c:\test\get-process.html
Same as Example 1, but now we will only turn a cell red if CPU is greater than 100
but less than 2000.
.EXAMPLE
$HTML = $Data | sort server | ConvertTo-html -head $header | Set-CellColor cookedvalue red -Filter "cookedvalue -gt 1"
PS C:\> $HTML = $HTML | Set-CellColor Server green -Filter "server -eq 'dc2'"
PS C:\> $HTML | Set-CellColor Path Yellow -Filter "Path -like ""*memory*""" | Out-File c:\Test\colortest.html
Takes a collection of objects in $Data, sorts on the property Server and converts to HTML. From there
we set the "CookedValue" property to red if it's greater then 1. We then send the HTML through Set-CellColor
again, this time setting the Server cell to green if it's "dc2". One more time through Set-CellColor
turns the Path cell to Yellow if it contains the word "memory" in it.
.EXAMPLE
$HTML = $Data | sort server | ConvertTo-html -head $header | Set-CellColor cookedvalue red -Filter "cookedvalue -gt 1" -Row
Now, if the cookedvalue property is greater than 1 the function will highlight the entire row red.
.NOTES
Author: Martin Pugh
Twitter: @thesurlyadm1n
Spiceworks: Martin9700
Blog: www.thesurlyadmin.com
Changelog:
1.5 Added ability to set row color with -Row switch instead of the individual cell
1.03 Added error message in case the $Property field cannot be found in the table header
1.02 Added some additional text to help. Added some error trapping around $Filter
creation.
1.01 Added verbose output
1.0 Initial Release
.LINK
http://community.spiceworks.com/scripts/show/2450-change-cell-color-in-html-table-with-powershell-set-cellcolor
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory,Position=0)]
[string]$Property,
[Parameter(Mandatory,Position=1)]
[string]$Color,
[Parameter(Mandatory,ValueFromPipeline)]
[Object[]]$InputObject,
[Parameter(Mandatory)]
[string]$Filter,
[switch]$Row
)
Begin {
Write-Verbose "$(Get-Date): Function Set-CellColor begins"
If ($Filter)
{ If ($Filter.ToUpper().IndexOf($Property.ToUpper()) -ge 0)
{ $Filter = $Filter.ToUpper().Replace($Property.ToUpper(),"`$Value")
Try {
[scriptblock]$Filter = [scriptblock]::Create($Filter)
}
Catch {
Write-Warning "$(Get-Date): ""$Filter"" caused an error, stopping script!"
Write-Warning $Error[0]
Exit
}
}
Else
{ Write-Warning "Could not locate $Property in the Filter, which is required. Filter: $Filter"
Exit
}
}
}
Process {
$InputObject = $InputObject -split "`r`n"
ForEach ($Line in $InputObject)
{ If ($Line.IndexOf("<tr><th") -ge 0)
{ Write-Verbose "$(Get-Date): Processing headers..."
$Search = $Line | Select-String -Pattern '<th ?[a-z\-:;"=]*>(.*?)<\/th>' -AllMatches
$Index = 0
ForEach ($Match in $Search.Matches)
{ If ($Match.Groups[1].Value -eq $Property)
{ Break
}
$Index ++
}
If ($Index -eq $Search.Matches.Count)
{ Write-Warning "$(Get-Date): Unable to locate property: $Property in table header"
Exit
}
Write-Verbose "$(Get-Date): $Property column found at index: $Index"
}
If ($Line -match "<tr( style=""background-color:.+?"")?><td")
{ $Search = $Line | Select-String -Pattern '<td ?[a-z\-:;"=]*>(.*?)<\/td>' -AllMatches
$Value = $Search.Matches[$Index].Groups[1].Value -as [double]
If (-not $Value)
{ $Value = $Search.Matches[$Index].Groups[1].Value
}
If (Invoke-Command $Filter)
{ If ($Row)
{ Write-Verbose "$(Get-Date): Criteria met! Changing row to $Color..."
If ($Line -match "<tr style=""background-color:(.+?)"">")
{ $Line = $Line -replace "<tr style=""background-color:$($Matches[1])","<tr style=""background-color:$Color"
}
Else
{ $Line = $Line.Replace("<tr>","<tr style=""background-color:$Color"">")
}
}
Else
{ Write-Verbose "$(Get-Date): Criteria met! Changing cell to $Color..."
$Line = $Line.Replace($Search.Matches[$Index].Value,"<td style=""background-color:$Color"">$Value</td>")
}
}
}
Write-Output $Line
}
}
End {
Write-Verbose "$(Get-Date): Function Set-CellColor completed"
}
}