|
Function Set-CellColor
{ $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 ofobjects 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 theServer cell to green if it's "dc2". One more time through Set-CellColor
turns the Path cell toYellow 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 cookedvalueproperty 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 colorwith -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 tohelp. 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, stoppingscript!"
Write-Warning$Error[0]
Exit
}
}
Else
{ Write-Warning "Could not locate$Property in the Filter, which is required. Filter: $Filter"
Exit
}
}
}
Process {
ForEach ($Line in$InputObject)
{ If($Line.IndexOf("(.*?)'-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 tolocate property: $Property in table header"
Exit
}
Write-Verbose"$(Get-Date): $Property column found at index: $Index"
}
If ($Line -match"(.*?)' -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): Criteriamet! Changing row to $Color..."
If ($Line-match "")
{ $Line = $Line -replace " |
|
|