Cleaning up NetApp SnapManager for Virtual Infrastructure snapshots in VMware vSphere can be a pain if you have large number of VMs being backed up by SMVI. In my case there are snapshots that are consistently left behind like so:
which just pile up as the days go on. I think this is some sort of bug in either vSphere API or the way NetApp handles snapshotting during the backup window.
To have these snapshots cleared up after the backup jobs run I have written the following PowerShell script to deal with the situation:
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 |
#Add VMware snap-in to PS console Add-PSSnapin VMware.VimAutomation.Core #Store creds and VC hostname (commented out) in a file and assign to $VICreds var #New-VICredentialStoreItem -host SPN-vCENTER-02.alex.com -user alex\svc_vmwarevsc_vcuser -password xxxxx -file D:\Scripts\CredFile.xml $VICreds = Get-VICredentialStoreItem -file "D:\Scripts\CredFile.xml" #Connect to vCenter Server Connect-VIServer -Server $VICreds.Host -User $VICreds.User -Password $VICreds.Password #Loop through all VMs and search for smvi_* snapshots 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 "smvi_*") { Write-Output "Snapshot $snapname on $vmname virtual machine" | Out-File -filePath D:\Scripts\VM_List_with_NetApp_Snapshots.txt -append -encoding utf8 remove-snapshot -snapshot $snap -confirm:$false } Else { Write-Output "No SMVI snapshots found on $vmname" | Out-File -filePath D:\Scripts\VM_List_without_NetApp_Snapshots.txt -append -encoding utf8 } } } #Email alert section #PS < 3.0 $item_list = Get-Content D:\Scripts\VM_List_with_NetApp_Snapshots.txt -Delimiter "`0" #PS => 3.0 #$item_list = Get-Content D:\Scripts\VM_List_with_NetApp_Snapshots.txt -Raw $emailSmtpServer = "10.73.1.203" $emailSubject = "NetApp SMVI snapshots ready to be committed to disk.." $Attach1 = "D:\Scripts\VM_List_with_NetApp_Snapshots.txt" $Attach2 = "D:\Scripts\VM_List_without_NetApp_Snapshots.txt" $emailBody = @" List of smvi_* snapshots about to be deleted: $item_list "@ Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -SmtpServer $emailSmtpServer -Attachments $Attach1, $Attach2 #Cleanup Remove-Item D:\Scripts\VM_List_with_NetApp_Snapshots.txt Remove-Item D:\Scripts\VM_List_without_NetApp_Snapshots.txt |
My scripts are saved under D:\Scripts on the vCentre server so that’s where everything runs from.
Before executing the script you will need to run the following to save vCenter hostname, username and password in a credentials file:
1 |
New-VICredentialStoreItem -host SPN-vCENTER-02.alex.com -user alex\svc_vmwarevsc_vcuser -password xxxxx -file D:\Scripts\CredFile.xml |
For email alert section to work you need to modify $emailFrom and $emailTo variables.
All that’s left is to schedule the script to run on a daily basis and never have this problem again!
Example email you should get in your inbox once the script has done its job:
HI Adrian, thank you very much and this script worked perfectly and as described, and ran it via powershell ISe
Virgilio, you are more than welcome – I am glad the script worked for you and did what was necessary 🙂