VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot

Remove old Snapshots

Hello,

I use to remove old snapshots with this script where I pute VM name and then run:

$vmlist = Get-Content C:\Users\gemela\Desktop\sv.txt

foreach($VM in $VMlist) {

Get-Snapshot -VM $vm |

Remove-Snapshot -Confirm:$false }

Disconnect-VIServer -Confirm:$false

I would like to set this script in a different way, to delete only snapshots older than X days except VM inside a txt list or except snapshots where name/description contain some specific word.

Thanks

1 Reply
LucD
Leadership
Leadership

Try something like this.
Remove the WhatIf when you are sure the correct snapshots are selected.

$vmlist = Get-Content C:\Users\gemela\Desktop\sv.txt 

$excludeVM = 'vm1','vm2','vm3'

$excludeText = 'word1','word2','word3'

$daysOld = 7

$tgtDate = (Get-Date).AddDays(- $daysOld)

$excludeTextRegEx = $excludeText -join '|'

Get-VM -Name $vmlist | Get-Snapshot |

where{$_.Created -le $tgtDate -and

      $excludeVM -notcontains $_.VM.Name -and

      ($_.Name -notmatch $excludeTextRegEx -and $_.Description -notmatch $excludeTextRegEx)} |

Remove-Snapshot -WhatIf -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference