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
Solved

PowerShell PowerCLI script for VMware Datastore

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

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 19

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
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 join ESXi hosts to Active Directory domains Open vSphere Client: Join ESXi host to AD domain: Verify ESXi computer account in AD: Configure permissions for domain user in ESXi: Test domain user login to ESXi host:

839 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