Solved

PowerShell Script to color a row based on a condition

Posted on 2014-03-28
7
2,603 Views
Last Modified: 2014-04-14
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
Comment
Question by:hchabria
  • 3
  • 2
7 Comments
 
LVL 18

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 39961263
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
 

Author Comment

by:hchabria
ID: 39961413
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 39961756
Move $html =   from line 21 to the beginning of line 20
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:hchabria
ID: 39961864
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
 
LVL 18

Assisted Solution

by:Raheman M. Abdul
Raheman M. Abdul earned 500 total points
ID: 39961891
From my last comment code, use the 2 lines which contains -replace
Change font to color
It should work
0
 

Author Comment

by:hchabria
ID: 39962052
Looks great now. Thank you for you assistance. Looking forward to color the entire row.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
This article will help you understand what HashTables are and how to use them in PowerShell.
Teach the user how to delpoy the vCenter Server Appliance and how to configure its network settings Deploy OVF: Open VM console and configure networking:
Teach the user how to convert virtaul disk file formats and how to rename virtual machine files on datastores. Open vSphere Web Client: Review VM disk settings: Migrate VM to new datastore with a thick provisioned (lazy zeroed) disk format: Rename a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now