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
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
$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
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
$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
Thanks for the help LucD, Works Great