Script to Output to CSV and Email Disk Sizes of VMs

I wanted to get a list of all disk sizes, insert this into a .csv file then email the file to a couple of recipients. Using Powershell, I wanted to get a list of all the production severs from a vCenter 5.5 server which were all located in a "Production" folder. 

 

 

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer -Server 172.17.5.1 -User 'jordansphere' -Password 'PASSWORD_HERE'


$VMs = Get-Folder 'Production' | Get-VM | Sort Name -descending
$Results = @()
foreach ($VM in $VMs) {
    $Result = new-object PSObject
    $Result | add-member -membertype NoteProperty -name "VM Name" -value $VM.Name
    $VMDiskCount = 1
    get-harddisk $VM | foreach {
        $disk = $_
        $Result | add-member -name "Disk($VMDiskCount)SizeGB" -value ([math]::Round($disk.CapacityKB / 1MB)) -membertype NoteProperty
        $VMDiskCount++
    }
    $Results += $Result
}

$Results | select-object * | export-csv -notypeinformation C:\temp\VCBDiskReport.csv

Send-MailMessage -from "Sender Name <[email protected]>" `
                       -to "Recipient One <[email protected]>", `
                           "Recipient Two <[email protected]>" `
                       -subject "Hard Drives - VM Sizes" `
                       -body "Please find the output of all Production VMs and Hard Disk sizes" `
                       -Attachment "C:\temp\VCBDiskReport.csv" -smtpServer smtp.jordansphere.co.uk