VMware Cloud Community
Juliancre
Contributor
Contributor
Jump to solution

Highlight values greater than a percentage

This Sritpt verify Datastorage and see the percentage to Used Space:

Get-Datastore | Select Name, @{N=”FreespaceGB”;E={“{0:n2}” -f ($_.FreespaceGB)}}, CapacityGB, @{N=”ProvisionedGB”;E={“{0:n2}” -f ($_.CapacityGB – $_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))}}, @{N='Used Space(%)';E={[math]::Round((($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100),1)}} | foreach {if ('Used Space(%)' + 20 -ge "90") {$_ -replace "<tr>","<tr bgcolor=yellow>"} elseif( -gt "95"){$_ -replace "<tr>","<tr bgcolor=red>"}else{$_} } | Export-Csv "C:\Scripts\checks.csv"

But i need highlight to Yellow if Used Space greater to 90% and Red if Used Space greater 95%. It doesn't work. 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That will not work I'm afraid, you are replacing HTML tags before the actual ConvertTo-Html cmdlet.
There are no HTML tags at that moment.

Also, you can't refer to properties after the ConvertTo-Html cmdlet, the stream is a number of text lines.

An easier way is to mark the calculated properties, and then inject the colour code after the ConvetTo-Html.

Something like this for example

Get-Datastore |
Select Name,
  @{N="FreespaceGB";E={"{0:n2}" -f ($_.FreespaceGB)}},
  CapacityGB,
  @{N="ProvisionedGB";E={"{0:n2}" -f ($_.CapacityGB - $_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))}},
  @{N='Used Space(%)';E={
    $free = [math]::Round((($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100),1)
    if($free +20 -gt 95){"##red$free"}
    elseif ($free + 20 -gt 90) { "##yellow$free" }
    else { "##green$free" }
  }} |
ConvertTo-Html -PreContent "<h2>Data Storage vCenter: </h2>" |
ForEach-Object {
  $_.Replace('##red', '<font color=red>').Replace('##yellow', '<font color=yellow>').Replace('##green', '<font color=green>')
} |
Out-String | Out-File -FilePath .\report.html
Invoke-Item -Path .\report.html


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

In a CSV file you can't do highlighting of rows and/or columns.

From your code it looks as if you took a snippet that produces HTML code.
There you can have highlighting with that method.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Juliancre
Contributor
Contributor
Jump to solution

Is correct my sript is HTML code, use CSV files to probe correct result. This is my real code:

$body2 = Get-Datastore | Select Name, @{N=”FreespaceGB”;E={“{0:n2}” -f ($_.FreespaceGB)}}, CapacityGB, @{N=”ProvisionedGB”;E={“{0:n2}” -f ($_.CapacityGB – $_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))}}, @{N='Used Space(%)';E={[math]::Round((($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100),1)}} | foreach {if ('Used Space(%)' + 20 -ge "90") {$_ -replace "<tr>","<tr bgcolor=yellow>"} elseif( -gt "95"){$_ -replace "<tr>","<tr bgcolor=red>"}else{$_} } | ConvertTo-Html -PreContent "<h2>Data Storage vCenter: </h2>" | Out-String

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That will not work I'm afraid, you are replacing HTML tags before the actual ConvertTo-Html cmdlet.
There are no HTML tags at that moment.

Also, you can't refer to properties after the ConvertTo-Html cmdlet, the stream is a number of text lines.

An easier way is to mark the calculated properties, and then inject the colour code after the ConvetTo-Html.

Something like this for example

Get-Datastore |
Select Name,
  @{N="FreespaceGB";E={"{0:n2}" -f ($_.FreespaceGB)}},
  CapacityGB,
  @{N="ProvisionedGB";E={"{0:n2}" -f ($_.CapacityGB - $_.FreespaceGB +($_.extensiondata.summary.uncommitted/1GB))}},
  @{N='Used Space(%)';E={
    $free = [math]::Round((($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100),1)
    if($free +20 -gt 95){"##red$free"}
    elseif ($free + 20 -gt 90) { "##yellow$free" }
    else { "##green$free" }
  }} |
ConvertTo-Html -PreContent "<h2>Data Storage vCenter: </h2>" |
ForEach-Object {
  $_.Replace('##red', '<font color=red>').Replace('##yellow', '<font color=yellow>').Replace('##green', '<font color=green>')
} |
Out-String | Out-File -FilePath .\report.html
Invoke-Item -Path .\report.html


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Juliancre
Contributor
Contributor
Jump to solution

Thanks, is Great.

0 Kudos