I expanded upon a useful sharing from over at the VMware community site.
Below is some code you can use to run against vcenter to get an emailed report of all virtual machines with low free space. The VMs need to have vmware tools running for this report. Don’t worry, I’ll post a script to check on that too!
As a personal preference I like to create a service account with logon as batch rights on a system to run this script. I then provision this same service account with read only privileges to vCenter. Once those things are setup the powershell script can be ran from task scheduler (using least privileged, yay).
Another thing I like to do is to capture transcripts and outputs of any scripts I run. This way if I have an SMTP issue or lose the email, I can go back and check files to see what was in previous reports or any errors that the script encountered.
You will want to edit the vcenter server name, the output file path, and email addresses before using this script. The script will email any VMs with drives that have less than 10% free space, of course, you can customize that % as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
$currentdate = (Get-Date).tostring("yyyy-MM-dd") $transfilename = 'OldSnapsTranscript_' + $currentdate + '.txt' $transfilepath = 'C:\scripts\Transcripts\' + $transfilename Start-Transcript $transfilepath -NoClobber -Append #Load PowerCLI snapins and dot source them Add-PSSnapin vmware.vimautomation.core . "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" #Connect to vcenter Connect-VIServer vcenterserver.company.com #Generating timestamped log name for powercli html output $outfilename = 'VMFreeSpace_' + $currentdate + '.htm' $outfilepath = 'C:\Scripts\Output\' + $outfilename $PoweredOnVMwithTools = Get-VM | Where-Object {($_.PowerState -eq "PoweredOn") -and (($_.Extensiondata.Guest.ToolsStatus -eq "toolsOk") -or ($_.Extensiondata.Guest.ToolsStatus -eq "toolsOld")) } $DriveReportArray = @() ForEach ($VM in $PoweredOnVMwithTools){ ForEach ($Drive in $VM.ExtensionData.Guest.Disk){ $Path = $Drive.DiskPath #Calculations $Freespace = [math]::Round($Drive.FreeSpace / 1MB) $Capacity = [math]::Round($Drive.Capacity/ 1MB) $SpaceOverview = "$Freespace" + "/" + "$capacity" $PercentFree = [math]::Round(($FreeSpace)/ ($Capacity) * 100) #VMs with less space if ($PercentFree -lt 10) { $newobj = New-Object -TypeName PSObject $newobj | Add-Member -Type NoteProperty -Name VM -Value $VM.name $newobj | Add-Member -Type NoteProperty -Name Disk -Value $Path $newobj | Add-Member -Type NoteProperty -Name FreeMB -Value $Freespace $newobj | Add-Member -Type NoteProperty -Name PercentFree -Value $PercentFree $DriveReportArray += $newobj } } # End ForEach ($Drive in in $VM.Extensiondata.Guest.Disk) } # End ForEach ($VM in $PoweredOnVMwithTools) if ($DriveReportArray) { $DriveReportArray | Sort-Object FreeMB | ConvertTo-Html | Out-File $outfilepath } #variables for email output $eto = "youremail@company.com" $efrom = "PowerCLI_Reports <PowerCLIReports@company.com>" $esubject = "Virtual Machine Guest Disks Low Free Space Report" If ($DriveReportArray) { $ebody = (Get-Content $outfilepath | Out-String) } $eserver = "smtpserver.company.com" If ($DriveReportArray) { Send-MailMessage -From $efrom -To $eto -Subject $esubject -BodyAsHtml -Body $ebody -smtpserver $eserver } Else { Send-MailMessage -From $efrom -To $eto -Subject $esubject -Body "There are no VMs with low free space in this report" -smtpserver $eserver } |
0 Comments.