I am trying to generate an HTML report that will make a line red if a threshold has been reached. Here is what I have and I am not sure of how to evaluate each line for a value of say (FreespaceGB -le 60.00) then make line red.
$File = "Default.htm"
# Check to see if the file exists
if (Test-Path $File)
{
Remove-Item $File
}
$Collection = @()
Get-Cluster | ForEach-Object {
$Cluster = $_
$Cluster | Get-VMHost | ForEach-Object {
$VMHost = $_
$VMHost | Get-DataStore | Where-Object { $_.Name -notlike "*local*"} | ForEach-Object {
$out = "" | Select-Object Cluster, DSName, FreespaceGB, CapacityGB, PercentFree
$out.Cluster = $Cluster.Name
$out.DSName = $_.Name
$out.FreespaceGB = $($_.FreespaceMB / 1024).tostring("F02")
$out.CapacityGB = $($_.CapacityMB / 1024).tostring("F02")
$out.PercentFree = (($_.FreespaceMB) / ($_.CapacityMB) * 100).tostring("F02") + "%"
$Collection += $out
}
}
}
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
$Collection | Sort-Object Cluster, DSName -Unique | ConvertTo-HTML -head $a | Out-File $File
There was a <CR><LF> missing after the copy/paste, that is corrected now.
That could also mean that the value in the FreeSpaceGB column is less than 60 for all rows.
Just tried it in my test environment and it works wthout a problem.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Have a look at HTML report, in that thread I provided a script to color-code a cell conditionally.
The trick was to fill a variable, $code in the script, with a specific color depending on a condition.
In the output array, the script then add the tag for the color.
$dateHtml = '<p style=''color:' + $code + ';''>' + $_.CreateTime + '</p>'
Finally the complete array is passed to the ConvertTo-Html cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
But my data is in a collection.
You add the conditional color tag during the creation of the collection.
Something like this
$File = "Default.htm" # Check to see if the file exists
if (Test-Path $File) { Remove-Item $File
} $Collection = @() Get-Cluster | ForEach-Object { $Cluster = $_
$Cluster | Get-VMHost | ForEach-Object { $VMHost = $_
$VMHost | Get-DataStore | Where-Object { $_.Name -notlike "*local*"} | ForEach-Object { $out = "" | Select-Object Cluster, DSName, FreespaceGB, CapacityGB, PercentFree
if($_.FreeSpaceMB/1KB -lt 60){ $out.Cluster = '<p style=''color:red;''>' + $Cluster.Name + '</p>'
$out.DSName = '<p style=''color:red;''>' + $_.Name + '</p>'
$out.FreespaceGB = '<p style=''color:red;''>' + $($_.FreespaceMB / 1024).tostring("F02") + '</p>'
$out.CapacityGB = '<p style=''color:red;''>' + $($_.CapacityMB / 1024).tostring("F02") + '</p>'
$out.PercentFree = '<p style=''color:red;''>' + (($_.FreespaceMB) / ($_.CapacityMB) * 100).tostring("F02") + "%" + '</p>'
} else{ $out.Cluster = $Cluster.Name $out.DSName = $_.Name $out.FreespaceGB = $($_.FreespaceMB / 1024).tostring("F02") $out.CapacityGB = $($_.CapacityMB / 1024).tostring("F02") $out.PercentFree = (($_.FreespaceMB) / ($_.CapacityMB) * 100).tostring("F02") + "%"
} $Collection += $out
} } } $a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>" $Collection | Sort-Object Cluster, DSName -Unique | ConvertTo-HTML -head $a | `
Out-String | foreach {$_.replace("<","<").replace(">",">")} | Out-File $File
Invoke-Item $File
Note that the ConvertTo-Html cmdlet converts are tag delimiters into $lt and $gt.
We need to replace these before writing the HTML code to the file
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
now everything is red.
There was a <CR><LF> missing after the copy/paste, that is corrected now.
That could also mean that the value in the FreeSpaceGB column is less than 60 for all rows.
Just tried it in my test environment and it works wthout a problem.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference