Solved

PowerShell Script to color a row based on a condition

Posted on 2014-03-28
7
3,515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 19

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 70

Expert Comment

by:Qlemo
ID: 39961756
Move $html =   from line 21 to the beginning of line 20
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 19

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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article outlines why you need to choose a backup solution that protects your entire environment – including your VMware ESXi and Microsoft Hyper-V virtualization hosts – not just your virtual machines.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Advanced tutorial on how to run the esxtop command to capture a batch file in csv format in order to export the file and use it for performance analysis. He demonstrates how to download the file using a vSphere web client (or vSphere client) and exp…
This video shows you how easy it is to boot from ISO images for virtual machines with the ISO images stored on a local datastore on the ESXi host.
Suggested Courses

630 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