?
Solved

PowerShell PowerCLI script for VMware Datastore

Posted on 2014-03-20
5
Medium Priority
?
1,449 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
[X]
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
  • 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 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Ransomware is a malware that is again in the list of security  concerns. Not only for companies, but also for Government security and  even at personal use. IT departments should be aware and have the right  knowledge to how to fight it.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Teach the user how to edit .vmx files to add advanced configuration options Open vSphere Web Client: Edit Settings for a VM: Choose VM Options -> Advanced: Add Configuration Parameters:
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

765 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