Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4074
  • Last Modified:

PowerShell Script to color a row based on a condition

I am using PowerShell PowerCLI script to report VMware Datastore status.

I am unable to put the condition that will check the Percent Free value so that if any of the values is less than 10%, the corresponding row will be marked with Red in color.

Can you please help me to do that by modifying the following code that I am using?

Add-PSSnapin "VMware.VimAutomation.core" | Out-Null
cls

## create the style HTML code
$a = "<style>"
$a = $a + "BODY{background-color:white; font-family:Tahoma; font-size:9pt;}"  
$a = $a + "TABLE{border-width: 1px;border-style: solid;boder-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;boder-color: black;background-color: Silver}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;boder-color: black;background-color: white}"
$a = $a + "</style>"

$date = get-date -uformat "%d-%m-%Y"

$Declaration = "<body>Hi All, <br><br> Please find below the WMware Datastore Report. Please take immediate action to increase the Datastore space having <b><font color=red><u>Less Than 15%</u></font></b> (Percent Free) space.<br><br>Note:<i> This is an auto-generated e-mail. Please do not reply.</i></br></br><br><br>"
$Heading1 = "<body><font color=blue size=5>VMware Datastore Report as on $date</font></body><br>"

## Datastore info gathering code
Connect-VIServer xxxx -Protocol https -User xxxxx -Password xxxx

Get-View -ViewType Datastore  | Where-Object {$_.Name -notlike "DataStore1*"} | 
     
Select-Object -Property Name,
  
@{N="FreeSpaceGB"; E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},

@{N="Capacity (GB)"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},

@{N="Percent Free"; E={[Math]::Round(($_.Summary.FreeSpace/$_.Summary.Capacity)*100)}},

@{N="Provisioned Space (GB)"; E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},

@{N="Over-Provisioned-DS"; E={([Math]::Round($_.Summary.Capacity/1GB,0)) - ([Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0))}} | Sort-Object -Property "Percent Free" | Select-Object Name,"Capacity (GB)","Provisioned Space (GB)","Percent Free" | ConvertTo-HTML -head $a -Title "Datastore Info" -Body $a$Declaration$Heading1 | out-file E:\PowerShell\Scripts\Production\VMWare\Datastore_Check_v1.0\DSReport.html

Open in new window

0
hchabria
Asked:
hchabria
  • 3
  • 2
2 Solutions
 
Raheman M. AbdulCommented:
Try this: ( it just changes the color of the percentfree value to red and not the entire row., still working on it)
Add-PSSnapin "VMware.VimAutomation.core" | Out-Null
cls

## create the style HTML code
$a = "<style>"
$a = $a + "BODY{background-color:white; font-family:Tahoma; font-size:9pt;}"  
$a = $a + "TABLE{border-width: 1px;border-style: solid;boder-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;boder-color: black;background-color: Silver}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;boder-color: black;background-color: white}"
$a = $a + "</style>"

$date = get-date -uformat "%d-%m-%Y"

$Declaration = "<body>Hi All, <br><br> Please find below the WMware Datastore Report. Please take immediate action to increase the Datastore space having <b><font color=red><u>Less Than 15%</u></font></b> (Percent Free) space.<br><br>Note:<i> This is an auto-generated e-mail. Please do not reply.</i></br></br><br><br>"
$Heading1 = "<body><font color=blue size=5>VMware Datastore Report as on $date</font></body><br>"

## Datastore info gathering code
Connect-VIServer xxxx -Protocol https -User xxxxx -Password xxxx

Get-View -ViewType Datastore  | Where-Object {$_.Name -notlike "DataStore1*"} | 
     $html = Select-Object -Property Name,
     @{N="FreeSpaceGB"; E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},
     @{N="Capacity (GB)"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},
     @{N="Percent Free"; E={ $pf= [Math]::Round(($_.Summary.FreeSpace/$_.Summary.Capacity)*100); if( $pf -lt 10) { "#color"+$pf+"color#" }  else { $pf }    }},

     @{N="Provisioned Space (GB)"; E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},

     @{N="Over-Provisioned-DS"; E={([Math]::Round($_.Summary.Capacity/1GB,0)) - ([Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0))}} | Sort-Object -Property "Percent Free" | Select-Object Name,"Capacity (GB)","Provisioned Space (GB)","Percent Free" | ConvertTo-HTML -head $a -Title "Datastore Info" -Body $a$Declaration$Heading1 

$html = $html -replace "#font","<font color='red'>"
$html = $html -replace "font#","</font>"
$html | out-file E:\PowerShell\Scripts\Production\VMWare\Datastore_Check_v1.0\DSReport.html

Open in new window

0
 
hchabriaAuthor Commented:
It's giving error in the line

$html = Select-Object -Property Name,

The error is-

Expressions are only allowed as the first element of a pipeline.
At C:\Users\xxx\AppData\Local\Temp\6\07d99490-ea23-46db-967c-549869eff2fc.ps1:22 char:7
+ $html  <<<< = Select-Object -Property Name,
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
0
 
QlemoC++ DeveloperCommented:
Move $html =   from line 21 to the beginning of line 20
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
hchabriaAuthor Commented:
I put this like below-

$html = Get-View -ViewType Datastore  | Where-Object {$_.Name -notlike "DataStore1*"} |

but getting the attached output. Here, values <10 are replaced by #color0color#.
DSTable.JPG
0
 
Raheman M. AbdulCommented:
From my last comment code, use the 2 lines which contains -replace
Change font to color
It should work
0
 
hchabriaAuthor Commented:
Looks great now. Thank you for you assistance. Looking forward to color the entire row.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now