Solved

PowerShell PowerCLI script for VMware Datastore

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
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:
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now