Solved

PowerShell PowerCLI script for VMware Datastore

Posted on 2014-03-20
5
1,406 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

If we need to check who deleted a Virtual Machine from our vCenter. Looking this task in logs can be painful and spend lot of time, so the best way to check this is in the vCenter DB. Just connect to vCenter DB(default DB should be VCDB and using…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Teach the user how to install vSphere Update Manager  Console to Windows system:  Install vSphere Update Manager: Configure vSphere Update Manager plug-in in vSphere Client: Verify vSphere Update Manager settings in vSphere Client:
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 …

770 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