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?
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.
Get-VM -Name $vmNames | Get-Snapshot | Remove-Snapshot -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
Or if i wanted to delete the snapshots based on what's in the title such as -like *NAMEHERE* ?
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.
$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
$vmNames = Get-Content -Path .\vmnames.txt
Get-VM -Name $vmNames | Get-Snapshot |
where{$_.Description -match $tgtText} |
Remove-Snapshot -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Tried this code, but it doesnt delete. Tried modifying it to " -like " instead of match and that doesnt work either.
any ideas?
What text did you specify, and what is in the Description field of the snapshot?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
I don't see any reason why the -match operator wouldn't work in that case.
$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
$vmNames = Get-Content -Path "FILEPATH\DeleteSnapShots.txt"
Get-VM -Name $vmNames | Get-Snapshot | where { $_.Description -like "*$tgtText*" }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference