Hi gurus!
Please help
I get all tree of snapshots:
get-snapshot myvm
Name Description PowerState
---- ----------- ----------
SNAP00 No Desc PoweredOff
SNAP00 No Desc PoweredOff
SNAP00 No Desc PoweredOff
In this case all names are identical!
So the condition:
If all names in snapshots tree are identical - delete all snapshot!
If not - do nothing.
You could do something like this.
If you also want to delete the snapshot when there are more than 3, change the -eq to -ge
Group-Object -Property {$_.VM.Name},Name -PipelineVariable group |
ForEach-Object -Process {
if($group.Group.Count -eq 3){
$vmName,$snapName = $group.Name.Split(',').Trim(' ')
Get-VM -Name $vmName | Get-Snapshot -Name $snapName |
Remove-Snapshot -Confirm:$false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could do something like this.
If you also want to delete the snapshot when there are more than 3, change the -eq to -ge
Group-Object -Property {$_.VM.Name},Name -PipelineVariable group |
ForEach-Object -Process {
if($group.Group.Count -eq 3){
$vmName,$snapName = $group.Name.Split(',').Trim(' ')
Get-VM -Name $vmName | Get-Snapshot -Name $snapName |
Remove-Snapshot -Confirm:$false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LUCD!
LucD!
I tested the script and came to the conclusion that all VM snapshots are being deleted, even if they have a different name.
Question 1: how can I modify the script so that it deletes all snapshots only if they have the same name?
Question 2, how can I specify a variable to the script so that the operator enters the snapshot name (it is obviously the same in the snapshot tree)
Did you assign a value to the variable $snapName?
You could do a Read-Host to let the operator assign a value.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this, but not working
Get snaps, names are identical
C:\> Get-VM 17-RDP | Get-Snapshot
Name Description PowerState
---- ----------- ----------va1 PoweredOff
va1 PoweredOff
Get-VM 17-RDP | Get-Snapshot |
Group-Object -Property {$_.VM.Name},Name -PipelineVariable groupForEach-Object -Process {
$snapName = "va1"
if($group.Group.Count -ge 1){$vmName,$snapName = $group.Name.Split(',').Trim(' ')
Get-VM -Name $vmName | Get-Snapshot -Name $snapName |
Remove-Snapshot -Confirm:$false
}
}
You don't have a pipeline symbol (|) at the end of the line with Group-Object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok! my mistake!
But again, all snapshots are deleted, even if the names are different. I think that the problem is in the condition, because here only the number of snapshots in the group is considered ($group.Group.Count -ge 1), but their names are not compared!
Get-VM 17-RDP | Get-Snapshot |
Group-Object -Property {$_.VM.Name},Name -PipelineVariable group |
ForEach-Object -Process {
$snapName = "va1"
if($group.Group.Count -ge 1){
$vmName,$snapName = $group.Name.Split(',').Trim(' ')
Get-VM -Name $vmName | Get-Snapshot -Name $snapName |
Remove-Snapshot -Confirm:$false
}
}
You will have to define a name for the target snapshot in that case.
Something like this.
Note that will remove the snapshot with that name, even if there is only 1 snapshot.
Get-VM -Name 17-RDP | Get-Snapshot |
Group-Object -Property {$_.VM.Name},Name -PipelineVariable group |
ForEach-Object -Process {
if($group.Group.Count -ge 1){
$vmName,$snapName = $group.Name.Split(',').Trim(' ')
if($snapName -eq $tgtSnapName){
Get-VM -Name $vmName | Get-Snapshot -Name $snapName |
Remove-Snapshot -Confirm:$false
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference