Solved

PowerShell Script to color a row based on a condition

Posted on 2014-03-28
7
2,790 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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Last article we focus in how to VMware: How to create and use VMs TAGs – Part 1 so before follow this article and perform the next tasks, you should read the first article how to create the TAG before using them in Veeam Backup Jobs.
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
Teach the user how to install and configure the vCenter Orchestrator virtual appliance Open vSphere Web Client: Deploy vCenter Orchestrator virtual appliance OVA file: Verify vCenter Orchestrator virtual appliance boots successfully: Connect to the …
This video shows you how to use a vSphere client to connect to your ESX host as the root user. Demonstrates the basic connection of bypassing certification set up. Demonstrates how to access the traditional view to begin managing your virtual mac…

895 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

13 Experts available now in Live!

Get 1:1 Help Now