VMware Cloud Community
swamynaveen
Enthusiast
Enthusiast

PowerCLi Script for Datastore usage report

Can some one please modify below script to get Datastore NAAID and freespaceGB.

##### Add VMWare Snanpin.

if(-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue))

{

   Add-PSSnapin VMware.VimAutomation.Core

}

$D = get-date -uformat "%m-%d-%Y-%H:%M" # To get a current date.

#This function generates a nice HTML output that uses CSS for style formatting.

function Generate-Report {

    Write-Output "<html><head><title></title><style type=""text/css"">.Error {color:#FF0000;font-weight: bold;}.Title {background: #0077D4;color: #FFFFFF;text-align:center;font-weight: bold;}.Normal-left {text-align:left;}.Normal {text-align:right;}.High {background: #FF0000;color: #FFFFFF;text-align:center;font-weight: bold;}.low {background: #008000;color: #FFFFFF;text-align:center;font-weight: bold;}.Medium {background: #FFA500;color: #FFFFFF;text-align:center;font-weight: bold;}.head {background: #808080;color: #FFFFFF;text-align:center;font-weight: bold;}</style></head><body><table border='1'><tr class=""Title""><td colspan=""7"">vCenter:$VCenterHosts -Datastore Usage Report -Date: $D</td></tr><tr class="head" ><td>Row  </td><td>Datastore  </td><td>Cluster </td><td>Capacity(GB)  </td><td>Used(GB)  </td><td>% Free  </td><td>% Used  </td></tr>"

                Foreach ($store in $report){ 

    $Row = $Row + 1

                                    

                    If ($Store.Percused -gt "80" )

                       {

                            Write-Output "<td class=""Normal-left"">$($Row)</td><td class=""Normal-left"">$($store.name)</td><td class=""Normal"">$($Store.Cluster)</td><td class=""Normal"">$($store.CapacityGB)</td><td class=""Normal"">$($store.UsedGB)</td><td class=""High"">$($store.PercFree)</td><td class=""High"">$($store.Percused)</td></tr> "

                        }

                    elseif (($store.Percused -gt "70") -and ($store.Percused -lt "80"))

                      {

                            Write-Output "<td class=""Normal-left"">$($Row)</td><td class=""Normal-left"">$($store.name)</td><td class=""Normal"">$($Store.Cluster)</td><td class=""Normal"">$($store.CapacityGB)</td><td class=""Normal"">$($store.UsedGB)</td><td class=""Medium"">$($store.PercFree)</td><td class=""Medium"">$($store.Percused)</td></tr> "

                          }  

                    else

                      {

                            Write-Output "<td class=""Normal-left"">$($Row)</td><td class=""Normal-left"">$($store.name)</td><td class=""Normal"">$($Store.Cluster)</td><td class=""Normal"">$($store.CapacityGB)</td><td class=""Normal"">$($store.UsedGB)</td><td class=""low"">$($store.PercFree)</td><td class=""low"">$($store.Percused)</td></tr> "

                      }   

                    

                }

               

        Write-Output "</table></body></html>"

    }

#Login details to vCenter

$username = 'xxxx'

$password = 'xxxxx'

#Current, Previous, Difference File information

$digits = 2

$Folder = 'Path'

#List of servers including Virtual Center Server.  The account this script will run as will need at least Read-Only access to Virtual Center

$VCenterHosts="vCenter"

#Initialise Array

$Report = @()

$Row=0

#Get all datastores and put them in alphabetical order

       

        foreach ($server in $VCenterHosts){

          

        # Use specific login details for the rest of servers in $serverlist

        Connect-VIServer $server -user $username -password $password

               

        Get-Datastore | Sort-Object Name | %{

            $Store = {} | Select Name, CapacityGB, UsedGB, PercFree, Percused, Cluster

            $Store.Name = $_.name

            $Store.CapacityGB = [math]::Round($_.capacityMB/1024,$digits)

            $Store.FreeSpaceGB = [math]::Round(($_.CapacityMB - $_.UsedMB)/1024,$digits)

            $Store.UsedGB = [math]::Round(($_.CapacityMB - $_.FreeSpaceMB)/1024,$digits)

            $Store.PercFree = [math]::Round(100*$_.FreeSpaceMB/$_.CapacityMB,$digits)

        $Store.Percused=[math]::Round(100*$Store.UsedGB/$Store.CapacityGB,$digits)

            $Store.Cluster=  %{(Get-VMHost -Datastore $_.name)}|Select-Object -ExpandProperty Parent -Unique

            $Report += $Store

                                }

        # Disconnect from Virtual Center

        Disconnect-VIServer -Server $server -Confirm:$False

        }

                                       

# Generate the report and email it as a HTML body of an email

Generate-Report > $Folder\$(get-date -format yyyy-MM-dd)_DatastoreReport.html

    IF ($Report -ne ""){

    $SmtpClient = New-Object system.net.mail.smtpClient

    $SmtpClient.host = "xxxxx"   #Change to a SMTP server in your environment

    $MailMessage = New-Object system.net.mail.mailmessage

    $MailMessage.from = "xxx@xx.com"   #Change to email address you want emails to be coming from

    $MailMessage.To.add("xxx@xxx.com")    #Change to email address you would like to receive emails.

    $MailMessage.IsBodyHtml = 1

    $MailMessage.Subject = " Datastore Usage Report: -$VCenterHosts"

    $MailMessage.Body = Generate-Report

    $SmtpClient.Send($MailMessage)}

Reply
0 Kudos
1 Reply
swamynaveen
Enthusiast
Enthusiast

LucD​, Could you please help me out to accomplish this

Thank you in Advance!!

Regards,

Swamy Naveen

Reply
0 Kudos