Solved

PowerShell PowerCLI script for VMware Datastore

Posted on 2014-03-20
5
1,397 Views
Last Modified: 2014-03-28
I am using following Powershell script for VMware datastore report. This script generates VMware datastore report in .html format.

I have put a condition that if percentage free space is less than 15%, that corresponding datastore name will be marked as red in color.

Unfortunately this is happening with some of the datastores not for all as you can see from the attached (.html output) file, couple of datastores with less than 15% free space are not marked in red.

Request you to help me out so that datastores with <15% free space can be marked as red in color.

Add-PSSnapin "VMware.VimAutomation.core" | Out-Null
cls
## create the style HTML code (using a here-string)
$a = @"
<style>    
body {background-color:Ivory; font-family:Tahoma; font-size:9pt;}  
td, th {border:1px solid black; border-collapse:collapse;}    
th {color:white; background-color:Blue;}    
table, tr, td, th {padding: 2px; margin: 0px}    
table {margin-left:70px;}</style>
"@
## Datastore info gathering code
Connect-VIServer xxxx -Protocol https -User xxxx -Password xxxx
function CalcPercent {     
     param(     
     [parameter(Mandatory = $true)]     
     [int]$InputNum1,     
     [parameter(Mandatory = $true)]     
     [int]$InputNum2)     
     $InputNum1 / $InputNum2*100 
}
#$datastores = Get-Datacenter "Datacenter01" | Get-Datastore  | Where-Object {$_.Name -notlike "DataStore1*"} 
$datastores = Get-Datastore  | Where-Object {$_.Name -notlike "DataStore1*"}   
ForEach ($ds in $datastores)
{      
	$PercentFree = CalcPercent $ds.FreeSpaceMB $ds.CapacityMB         
        $PercentFree = "{0:N2}" -f $PercentFree        
        $ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree    
} 

##create a TABLE from the gathered info
$strTableStartHTML1 = "<body>Hi All, <br><br> Please find below the WMware Datastore Report. Please note that the <b>Free Space</b> of Datastore having <b>Less Than 15%</b> will be marked as <font color = red>Red</font>. In that case please take immediate action to increase the space of those Datastores. <br><br>Note:<i> This is an auto-generated e-mail. Please do not reply.</i></br></br><br><br>"
$strTableStartHTML = "<TABLE CELLSPACING=1 CELLPADDING=1 BORDER=1>`n<TR><TH>Datastore Name</TH><TH>Capacity (GB)</TH><TH>Percent Free</TH></TR>"
$strTableBodyHTML = foreach ($dStore in $datastores) {    

## create the row STYLE HTML based on this datastore's PercentFree value    
$strRowStyleHTML = if ($dStore.PercentFree -lt 15) {" STYLE='background-color: red'"} else {$null}    

## return the HTML for the row for this datastore    
"<TR$strRowStyleHTML><TD>$($dStore.Name)</TD><TD>$($dStore.CapacityGB)</TD><TD>$($dStore.PercentFree)</TD></TR>`n"
} ## end foreach
$strTableEndHTML = "</TABLE></br></br><br>Thank You.</body>"

ConvertTo-Html -Title "Datastores Info" -Body $a$strTableStartHTML1$strTableStartHTML$strTableBodyHTML$strTableEndHTML | Out-File E:\PowerShell\Scripts\Production\VMWare\Datastore_Check\DStoreInfo.html

Open in new window

html-output.png
0
Comment
Question by:hchabria
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39944934
Remove this line and see if it works.

$PercentFree = "{0:N2}" -f $PercentFree        

You are converting by formatting to string value from the double value.
0
 

Author Comment

by:hchabria
ID: 39945171
After removing the following it's working fine. Now all datastores having <15% free space are coming under red mark.

$PercentFree = "{0:N2}" -f $PercentFree

But the problem is that the "Percent Free" value has been extended after decimal point. Before deleting the line it was showing as 4.11%, but after deleting the line it is showing 4.1135564186302%.

Please help me to resolve this.
0
 
LVL 18

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 39958478
Replace the following line:
$PercentFree = "{0:N2}" -f $PercentFree

with this:
$PercentFree =[System.Math]::Round($PercentFree,2)



Or replace the function with
function CalcPercent {    
     param(    
     [parameter(Mandatory = $true)]    
     [int]$InputNum1,    
     [parameter(Mandatory = $true)]    
     [int]$InputNum2)    
     [System.Math]::Round($InputNum1 / $InputNum2*100 )
}

In the above function the result is rounded to 2 decimal places.
0
 

Author Closing Comment

by:hchabria
ID: 39961141
Thanks for providing me a perfect solution.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Teach the user how to configure vSphere clusters to support the VMware FT feature Open vSphere Web Client: Verify vSphere HA is enabled: Verify netowrking for vMotion and FT Logging is in place or create it: Turn On FT for a virtual machine: Verify …
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.

863 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

27 Experts available now in Live!

Get 1:1 Help Now