The PowerShell script below will email a list of Vcenter Snapshots. It utilises the VMWare PowerCLI which must be installed on the machine executing the script.
# Install-Module -Name VMware.PowerCLI -Allowclobber
# connect-viserver vcenter-hostname
# Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false
# Set-PowerCLIConfiguration -DefaultVIServerMode single -Confirm:$false
# Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
# New-VICredentialStoreItem -Host vcenter-hostname -User [email protected] -Password xxxxxxx -File vccred.xml
# Vcenter host`
$hostname = "vcenter.domain.local"
# Vcenter user
$username = "[email protected]"
# Credential file storing password
$filename = "vccred.xml"
# Mail recipients
$sendto = "[email protected]"
$cred = Get-VICredentialStoreItem -User $username -Host $hostname -File $filename
Connect-VIServer -Server $cred.Host -User $cred.User -Password $cred.Password | Out-Null
function Get-SnapChild{
param(
[VMware.Vim.VirtualMachineSnapshotTree]$Snapshot
)
process{
$snapshot.Name
if($Snapshot.ChildSnapshotList.Count -gt 0){
$Snapshot.ChildSnapshotList | %{ Get-SnapChild -Snapshot $_ }
}
}
}
$count = 0
foreach($vm in Get-View -ViewType VirtualMachine -Property Name,Snapshot -Filter @{'Snapshot' = ''}) {
Write-Host "VM: "+$vm.name+"" -foreground green
$body = $body + "<P><H3>VM: "+$vm.name+"</H3><UL>"
foreach($ssl in $vm.Snapshot.RootSnapshotList) {
foreach($ssn in $ssl) {
$ssn
$body = $body + (ConvertTo-Html -InputObject $ssn -As List -Fragment) + "<br />"
}
}
$snapshots += Get-SnapChild -Snapshot $_
$body = $body + "</UL>"
$count = $count + 1
}
# Only send email notification if there are any snapshots
If ($count -gt 0) {
$body = "<body>" + ("$($count) snapshots." | out-string) + ("" | Out-String) + $body + "</body>"
Send-MailMessage -To $sendto -Subject "VM Snapshots - Remove snapshot if not required" -Priority high -Smtpserver smtpout.romec.local -From [email protected] -Body $body -BodyAsHtml
Write-Host "Email sent to "$sendto
}
The script then can be run as a scheduled task to notify a group of VMWare admins.