VMware Cloud Community
ajkbond
Contributor
Contributor
Jump to solution

Delete old snapshot but exclude with snapshot name do not delete

I have below script which will delete snapshot older than 15 days, what i need is to add a parameter like get-snapshot or something to exclude snaphsot with name "donotdelete"

# vCenter Server configuration

$vcenter = "xxxxxxxx"

$vcenteruser = "administrator@vsphere.local"

$vcenterpw = "xxxxxxx"

#Connect to the vCenter server defined above. Ignore certificate errors

connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'

Clear-Host

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-15)} | Remove-Snapshot -Confirm:$false

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.
Btw why do you still have a Add-PSSnapin in there?

Which PowerCLI version are you using?

# vCenter Server configuration

$vcenter = "xxxxxxxx"

$vcenteruser = "administrator@vsphere.local"

$vcenterpw = "xxxxxxx"


#Connect to the vCenter server defined above. Ignore certificate errors

connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'

Clear-Host

Get-VM | Get-Snapshot |

Where {$_.Created -lt (Get-Date).AddDays(-15) -and $_.Name -notmatch "donotdelete"} |

Remove-Snapshot -Confirm:$false


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this.
Btw why do you still have a Add-PSSnapin in there?

Which PowerCLI version are you using?

# vCenter Server configuration

$vcenter = "xxxxxxxx"

$vcenteruser = "administrator@vsphere.local"

$vcenterpw = "xxxxxxx"


#Connect to the vCenter server defined above. Ignore certificate errors

connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'

Clear-Host

Get-VM | Get-Snapshot |

Where {$_.Created -lt (Get-Date).AddDays(-15) -and $_.Name -notmatch "donotdelete"} |

Remove-Snapshot -Confirm:$false


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

0 Kudos
ajkbond
Contributor
Contributor
Jump to solution

Thanks LucD! working as expected.

0 Kudos
MsV_R
Contributor
Contributor
Jump to solution

It works but not able to filter based on description  "-and $_.Name -notmatch "donotdelete"} 

0 Kudos