Datastore Size Reporting via PowerCLI Script

1 minute read

After pulling together some different resources from the likes of Alan Renouf, Luc Dekens, Eric Sloof, and others in the PowerCLI community (Forgive me if I didn’t mention all but I read through a lot of different people’s posts!), I have pieced together something that I use regularly to report datastore sizes and amounts of free space.  All sent to you in a nice HTML e-mail.

    Title: Report-Datastores.ps1
    Description: Report Datastore usage for all Datastores managed by vCenter
    Requirements: Windows Powershell and the VI Toolkit
    Usage: .\Report-Datastores.ps1
    #Global Functions
    #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;}</style></head><body><table><tr class=""Title""><td colspan=""4"">VMware Datastore Report</td></tr><tr class="Title"><td>Datastore </td><td>Capacity(GB) </td><td>Used(GB) </td><td>% Free </td></tr>"
     Foreach ($store in $report){
     Write-Output "<td class=""Normal-left"">$($</td><td class=""Normal"">$($store.CapacityGB)</td><td class=""Normal"">$($store.UsedGB)</td><td class=""Normal"">$($store.PercFree)</td></tr> " 
     Write-Output "</table></body></html>" 
    #Login details
    $username = 'administrator@vsphere.local'
    $password = 'vmware'
    #Current, Previous, Difference File information
    $digits = 2
    $Folder = 'C:\Reports'
    #List of servers including Virtual Center Server. The account this script will run as will need at least Read-Only access to Cirtual Center
    $VIServers = "" #Chance to DNS Names/IP addresses of your ESXi servers or Virtual Center Server
    #Initialise Array
    $Report = @()
    #Get all datastores and put them in alphabetical order
     foreach ($server in $VIServers){
     # Check is server is a Virtual Center Server and connect with current user
     if ($server -eq "VCServer"){Connect-VIServer $VIServer}
     # Use specific login details for the rest of servers in $serverlist
     else {Connect-VIServer $VIServer -user $username -password $password}
     Get-Datastore | Sort-Object Name | %{
     $Store = {} | Select Name, CapacityGB, UsedGB, PercFree
     $Store.Name = $
     $Store.CapacityGB = [math]::Round($_.capacityMB/1024,$digits)
     $Store.UsedGB = [math]::Round(($_.CapacityMB - $_.FreeSpaceMB)/1024,$digits)
     $Store.PercFree = [math]::Round(100*$_.FreeSpaceMB/$_.CapacityMB,$digits)
     $Report += $Store
     # Disconnect from Virtual Center
     Disconnect-VIServer -Confirm:$False
    # Generate the report and email it as a HTML body of an email
    Generate-Report > "$Folder\Report-Datastore.html"
     IF ($Report -ne ""){
     $SmtpClient = New-Object
     $ = "" #Change to a SMTP server in your environment
     $MailMessage = New-Object
     $MailMessage.from = "" #Change to email address you want emails to be coming from
     $MailMessage.To.add("") #Change to email address you would like to receive emails.
     $MailMessage.IsBodyHtml = 1
     $MailMessage.Subject = "VMware Datastore Report"
     $MailMessage.Body = Generate-Report

Leave a Comment