Saturday, November 14, 2015

Version 2 VM disk space detail send via email Powercli

1 year back I had written this script, and it became very much popular, Collect disk space detail and send it in email Powercli since then I started receiving emails for some more additions in the existing script. I am not a HTML guy but still tried to create one, Many of the people found they don't have to login each server to collect disk information, They just need to login vCenter (Powercli) which saves lots of time. Also to work this script correctly make sure all your VMs has VMWare Tools installed and running. Also my earlier script was pulling reports only for Windows and C drive.
I have improvised it little bit and added below information.
  • Information for all vmware vm disk usage,
  • Any disk has below 20% will be in Red.

This is the screenshot how collected information will looks like in the email.

Once you execute script you will see below results on the screen which can tell you from what vm and disk it is collecting information. I am running this one Windows server 2012 R2, Powercli version 5.5, vsphere 5.5.
Below is the code information
  #####################################    
  ## http://kunaludapi.blogspot.com    
  ## Version: 2    
  ## Tested this script on    
  ## 1) Powershell v4    
  ## 2) Powercli v5.5    
  ## 3) Vsphere 5.x    
  ##   Find and input your information in below lines in the script  
      ############################################################  
  ##   $vCenterServer = "vCenterserver"   
  ##   $vcenteruser = "domain\user"   
  ##   $vcenterpasswd = "Password"   
  ##       
  ##   From = "SpaceAlert@email.com"   
  ##   To = "Employee@email.com"   
  ##   SmtpServer = "smtp.exchange.com"   
  #####################################   
 begin {  
 Add-PSSnapin vmware.vimautomation.core   
 #vCenter username password   
 $vCenterServer = "vCenterserver"   
 $vcenteruser = "domain\user"   
 $vcenterpasswd = "Password"   
 #connect vcneter server    
 Connect-VIServer -Server $vCenterServer -User $vcenteruser -Password $vcenterpasswd   
  $FinalResult = @()   
  $Allvms = Get-View -ViewType “VirtualMachine” -filter @{”Guest.GuestState”=”running”}   
 } #begin  
 Process {  
 foreach ($vm in $Allvms) {   
     $Drives = $vm.Guest.Disk    
     Write-host "Collecting Information from $($vm.name)" -ForegroundColor Green  
     Foreach ($DriveC in $drives) {    
     #Calculations   
     $Freespace = [math]::Round($DriveC.FreeSpace / 1MB)   
     $Capacity = [math]::Round($DriveC.Capacity / 1MB)   
     $SpaceOverview = "$Freespace" + "/" + "$capacity"   
     $PercentFree = [math]::Round(($FreeSpace)/ ($Capacity) * 100)  
     $Disk = $DriveC.DiskPath.Substring(0,2)  
     #Report for all vms   
     if ($PercentFree -lt 20) {  
       $vmnameRD = "<tr><td bgcolor='#ff704d'>{0}</td>" -f $vm.name  
       $DiskRD = "<td bgcolor='#ff704d'>{0}</td>" -f $Disk  
       $FreesspaceRD = "<td bgcolor='#ff704d'>{0}</td>" -f $Freespace  
       $capacityRD= "<td bgcolor='#ff704d'>{0}</td>" -f $capacity  
       #$SpaceOverviewRD = "<td bgcolor='#ff704d'>{0}</td>" -f $SpaceOverview  
       $PercentFreeRD = "<td bgcolor='#ff704d'>{0}</td></tr>" -f $PercentFree  
       $finalResult += $vmnameRD  
       $finalResult += $DiskRD  
       $finalResult += $FreesspaceRD  
       $finalResult += $capacityRD        
       #$finalResult += $SpaceOverviewRD  
       $finalResult += $PercentFreeRD  
       Write-Host "`t `tCollected from $($DriveC.DiskPath) on $($vm.name)" -ForegroundColor Yellow  
     }#if  
     else {  
       $vmnameNL = "<tr><td>{0}</td>" -f $vm.name  
       $DiskNL = "<td>{0}</td>" -f $Disk  
       $FreesspaceNL = "<td>{0}</td>" -f $Freespace  
       $capacityNL= "<td>{0}</td>" -f $capacity  
       #$SpaceOverviewNL = "<td>{0}</td>" -f $SpaceOverview  
       $PercentFreeNL = "<td>{0}</td></tr>" -f $PercentFree  
       $finalResult += $vmnameNL  
       $finalResult += $DiskNL  
       $finalResult += $FreesspaceNL  
       $finalResult += $capacityNL  
       #$finalResult += $SpaceOverviewNL  
       $finalResult += $PercentFreeNL  
       Write-Host "`t `tCollected from $($DriveC.DiskPath) on $($vm.name)" -ForegroundColor Yellow  
     }#else  
   } #foreach drive  
 } #foreach vm  
 #region body  
 $Body = @"  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head>  
 <style>BODY{background-color:#ffffff;}  
 TABLE{border-width: 1px;border-style: solid;border-color: #808080;border-collapse: collapse;}  
 TH{border-width: 1px;padding: 0px;border-style: solid;border-color: #808080;background-color:#808080;color: white;}  
 TD{border-width: 1px;padding: 0px;border-style: solid;border-color: #808080;}</style>  
 </head><body>  
 <H2>VMs DiskSpace usage report</H2>  
 <table><colgroup><col/><col/><col/><col/><col/></colgroup>  
 <tr><th>VMName</th>  
 <th>Disk</th>  
 <th>Free(MB)</th>  
 <th>Total(MB)</th>  
 <th>%Free</th></tr>  
 $finalResult   
 </table>  
 </body></html>  
 "@  
 #endregion body  
 }#process   
 end {    
  #Send Email   
  $messageParameters = @{   
  Subject = "Alert! VMs with less than 20% on diskspace marked Red"  
  Body = $Body  
  From = "SpaceAlert@email.com"   
  To = "Employee@email.com"   
  SmtpServer = "smtp.exchange.com"   
  }  
 Send-MailMessage @MessageParameters -BodyAsHtml
 }  

Check earlier blog:
Collect disk space detail and send it in email Powercli

vSphere Datastores inventory report powercli - Volume 2

Post a Comment