Thursday, May 22, 2014

Remove VMware snapshots one at a time

Commvault Simpana / NetApp SnapProtect has a nasty habit of creating a many snapshots if the job doesn't complete successfully.

Here is a script that will iterate through vCenter and remove all the SnapProtect snapshots - one snapshot at a time.


$vcenter = Read-Host "vCenter Server"
$vcuser = Read-Host "Username"
$vcpass = Read-Host "Password" -AsSecureString:$true
$vccred = New-Object System.Management.Automation.PSCredential -ArgumentList $vcuser,$vcpass
Connect-VIServer -server $vcenter -Credential $vccred
foreach ($vm in get-vm | sort Name) {
                $vmname = $vm.name
                $snaps = get-snapshot -vm $vm 
                foreach ($snap in $snaps) {
                                $snapName = $snap.name
                                if ($snapname -like "__GX_BACKUP__") {
                                                Write-Host "Found snapshot: $snapname on $vmname" -foregroundcolor red 
                                               
                                                remove-snapshot -snapshot $snap -confirm:$false
                                }
                                Else {
                                                Write-Host "No snapshots found on $vmname" -foregroundcolor green 
                                }
                }
}

Here is a modified version, which will remove all snaps - not just ones from SnapProtect.
$vcenter = Read-Host "vCenter Server"
$vcuser = Read-Host "Username"
$vcpass = Read-Host "Password" -AsSecureString:$true
$vccred = New-Object System.Management.Automation.PSCredential -ArgumentList $vcuser,$vcpass
Connect-VIServer -server $vcenter -Credential $vccred
foreach ($vm in get-vm | sort Name) {
 $vmname = $vm.name
 $snaps = get-snapshot -vm $vm 
 foreach ($snap in $snaps) {
  remove-snapshot -snapshot $snap -confirm:$false
 }
}

1 comment:

Featured Post

Remove 3D Objects and other annoying folders on Windows 10

 Microsoft just keeps adding more crap to clutter up the navigation in Windows 10.  Seriously, who needs a 3D Objects folder?  The tiny perc...