Removing vCenter Snapshots with PowerShell and VMware vSphere PowerCLI
24. November 2016
PowerShell Version: >4
Modules: VMware vSphere PowerCLI
While having issues with HP Data Protector and VMware backups I created a script that logs in to different vCenter Servers and checks for Data Protector Snapshots that are older than x and removes them. This script can easily be customized to suite your environment and needs.
# variables $name = "_DP_VEPA_SNAP_" $hours = "0" $vcenters = "VCENTER1-FQDN","VCENTER2-FQDN" $vc_username = "USERNAME" $vc_password = "PASSWORD" # load powercli commands add-pssnapin vmw* | out-null # for every vcenter foreach ($vcenter in $vcenters) { # connect Connect-VIServer -Server $vcenter -User $vc_username -Password $vc_password -WarningAction SilentlyContinue $snapshots = Get-Snapshot -vm * -name "$name" | Where {$_.Created -lt (Get-Date).AddHours($hours)} if ($? -eq $false) { "### $vcenter ###" "No matching Snapshots found." } else { "### $vcenter ###" $snapshots | select VM, @{Name="Age";Expression={((Get-Date)-$_.Created).Hours}}, SizeMB | Format-Table -Autosize # remove snapshots "Deleting..." $snapshots | Remove-Snapshot -confirm:$false "Done!" } # disconnect Disconnect-VIServer -Confirm:$False | out-null }