VMware Cloud Community
ajkbond
Contributor
Contributor
Jump to solution

Snapshot Script

i am using script below to delete snapshots older than 5 days by excluding "donotdelete" snapshots, my question is how i will add more names to exclude or read from a file to exclude the snapshot names.

if its from a file its easy as i can ask users to update the txt file if the want to exclude some snapshots.

$vcenter = "hostname"

$creds = Get-VICredentialStoreItem -file "C:\Vmware script\pass.cred"

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

connect-viserver $vcenter -User $creds.User -Password $creds.Password -WarningAction 0

Clear-Host

Get-VM | Get-Snapshot |Where {$_.Created -lt (Get-Date).AddDays(-5) -and $_.Name -notmatch "donotdelete" } | Remove-Snapshot -Confirm:$false

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

A RegEx expression, which is used on the right side operand in a -match/-notmatch expression, can use the RegEx or symbol ('|')

Something like this

$vcenter = "hostname"

$creds = Get-VICredentialStoreItem -file "C:\Vmware script\pass.cred"


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

connect-viserver $vcenter -User $creds.User -Password $creds.Password -WarningAction 0


$excludeNamesRegEx = "donotdelete|donttouch|stayaway"


Clear-Host

Get-VM | Get-Snapshot |

Where {$_.Created -lt (Get-Date).AddDays(-5) -and $_.Name -notmatch $excludeNamesRegEx } |

Remove-Snapshot -Confirm:$false


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

A RegEx expression, which is used on the right side operand in a -match/-notmatch expression, can use the RegEx or symbol ('|')

Something like this

$vcenter = "hostname"

$creds = Get-VICredentialStoreItem -file "C:\Vmware script\pass.cred"


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

connect-viserver $vcenter -User $creds.User -Password $creds.Password -WarningAction 0


$excludeNamesRegEx = "donotdelete|donttouch|stayaway"


Clear-Host

Get-VM | Get-Snapshot |

Where {$_.Created -lt (Get-Date).AddDays(-5) -and $_.Name -notmatch $excludeNamesRegEx } |

Remove-Snapshot -Confirm:$false


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

Reply
0 Kudos
ajkbond
Contributor
Contributor
Jump to solution

Thanks for the help LucD, Works Great

Reply
0 Kudos