Automation

 View Only
  • 1.  Snapshot Script

    Posted May 28, 2020 02:10 AM

    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



  • 2.  RE: Snapshot Script
    Best Answer

    Posted May 28, 2020 05:14 AM

    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



  • 3.  RE: Snapshot Script

    Posted May 28, 2020 01:47 PM

    Thanks for the help LucD, Works Great