We all know it's bad to leave snapshots lying around in VMware. Snapshots grow over time and can cause disk space and performance issues. Sometimes it's easy to forget that you created them. So you should periodically check to see what snapshots are out there. PowerCLI (PowerShell with the VMware snapin) provides a great way to do this.
The following script looks for snapshots on each VM in a vCenter environment. If the snapshot is 3 day old (or older), it will delete it. Well, I've got the -WhatIf switch on the remove command, so it won't actually delete anything unless you remove -WhatIf. You can also remove the -Confirm switch so it won't prompt you as it goes.
I put this together from bits and pieces I found on the web, so thanks for the help y'all.
$vcenter = "my-vcenter" $maxAge = (Get-Date).AddDays(-3) if (get-pssnapin vmware*) { # we're good } else { Add-pssnapin VMWare.VimAutomation.Core } Connect-VIServer $vcenter $found=0 Get-VM | Foreach-Object { $vmname = $_.Name Get-Snapshot -VM $_ | Foreach-Object { $snapshot = $_.Id $size = "{0:N3}" -f $_.SizeGB if($_.Created -lt $maxAge) { if($found -eq 0){ $found = 1 "`nVM Name `tSnapShot ID `tSize (GB)`tDate Created" "----------------------------------------------------------------------------" } "$vmname`t$snapshot`t$size`t" + $_.Created Remove-Snapshot $_ -Confirm -WhatIf } } } if($found -eq 0){ "`nNo old snapshots found!" } Disconnect-VIServer -Confirm:$False
0 comments:
Post a Comment