Automation

 View Only
Expand all | Collapse all

Delete-Snapshot Created on Specific Date from a list of VMs

  • 1.  Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 22, 2019 05:56 PM

    How would i do this? I am creating snapshots just incase I need them. I want to easy way to delete these from the list of VMs I have.

    Any ideas?



  • 2.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 22, 2019 06:17 PM

    In it's simplest form, you could do something like this.

    It assumes you have the names of the VMs in a .txt file, one name per line.

    $vmNames = Get-Content -Path .\vmnames.txt

    Get-VM -Name $vmNames | Get-Snapshot | Remove-Snapshot -Confirm:$false



  • 3.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 22, 2019 08:01 PM

    Thanks i have this simple piece of code already. Im looking to specify a date so that i dont have to worry about deleting the wrong snapshot.



  • 4.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 22, 2019 08:59 PM

    If you want to limit this to only snapshots created on a specific date, you could do.

    Note that the format in which you supply the date (dd/mm/yy or mm/dd/yy) depends on the regional settings on the station.

    $targetDate = '11/07/19'

    $date = Get-Date $targetDate

    $vmNames = Get-Content -Path .\vmnames.txt


    Get-VM -Name $vmNames | Get-Snapshot |

    where{$_.Created.Date -eq $date} |

    Remove-Snapshot -Confirm:$false


    When you want to limit the removal to snapshots with a specific text in the Description, you could do

    $tgtText = 'Some text'

    $vmNames = Get-Content -Path .\vmnames.txt


    Get-VM -Name $vmNames | Get-Snapshot |

    where{$_.Description -match $tgtText} |

    Remove-Snapshot -Confirm:$false



  • 5.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 22, 2019 10:12 PM

    Tried this code, but it doesnt delete. Tried modifying it to " -like " instead of match and that doesnt work either.

    any ideas?



  • 6.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 22, 2019 10:15 PM

    What text did you specify, and what is in the Description field of the snapshot?



  • 7.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 22, 2019 10:26 PM

    Here is what i have:

    $tgtText = 'August'

    $vmNames = Get-Content -Path "FILEPATH\DeleteSnapShots.txt"

    Get-VM -Name $vmNames | Get-Snapshot | where{$_.Description -like $tgtText} | Remove-Snapshot -Confirm:$false

    Here's whats in vCenter on the VM I added to the list below:

    Id                : VirtualMachineSnapshot-snapshot-197033

    Name              : August patches - VM Snapshot 8%252f22%252f2019, 9:20:40 AM



  • 8.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 23, 2019 05:31 AM

    I don't see any reason why the -match operator wouldn't work in that case.

    $tgtText = 'August'

    $vmNames = Get-Content -Path "FILEPATH\DeleteSnapShots.txt"

    Get-VM -Name $vmNames | Get-Snapshot | where { $_.Description -match $tgtText }

    If you insist on using the -like operator, which has fewer options compared to the -match operator, you will have to add meta-characters to the string.

    Something like this

    $tgtText = 'August'

    $vmNames = Get-Content -Path "FILEPATH\DeleteSnapShots.txt"

    Get-VM -Name $vmNames | Get-Snapshot | where { $_.Description -like "*$tgtText*" }



  • 9.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 23, 2019 05:33 PM

    Tried it again this morning. Still wont work. Doesnt delete the snapshot.

    I tried placing the VM name inline and it still wont delete the snapshot.



  • 10.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 23, 2019 08:45 PM

    That would mean that the following does not list any snapshots?

    $tgtText = 'Some text'

    $vmNames = Get-Content -Path .\vmnames.txt

    Get-VM -Name $vmNames | Get-Snapshot |

    where{$_.Description -match $tgtText}



  • 11.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 23, 2019 08:49 PM

    there are 2 snapshots on this VM. If the name was spelled incorrectly I would see an error. I think the pipe is not working.



  • 12.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 23, 2019 09:22 PM

    How do you run this snippet?
    From a .ps1 file, or do you copy the lines to the PS prompt?
    Could you attach the .ps1 file or share a screenshot?



  • 13.  RE: Delete-Snapshot Created on Specific Date from a list of VMs

    Posted Aug 22, 2019 08:20 PM

    Or if i wanted to delete the snapshots based on what's in the title such as -like *NAMEHERE* ?