Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PowerShell Script to color a row based on a condition

Posted on 2014-03-28
7
Medium Priority
?
3,801 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 2000 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 71

Expert Comment

by:Qlemo
ID: 39961756
Move $html =   from line 21 to the beginning of line 20
0
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this article we will learn how to backup a VMware farm using Nakivo Backup & Replication. In this tutorial we will install the software on a Windows 2012 R2 Server.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Teach the user how to configure vSphere Replication and how to protect and recover VMs Open vSphere Web Client: Verify vsphere Replication is enabled: Enable vSphere Replication for a virtual machine: Verify replicated VM is created: Recover replica…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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