VMware Cloud Community
IKirill
Enthusiast
Enthusiast
Jump to solution

Get-snapshot and the condition if

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.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

Get-VM | Get-Snapshot |

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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

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

Get-VM | Get-Snapshot |

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

Reply
0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

Thanks LUCD!

Reply
0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

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)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

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 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
            }
}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

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

}

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.

$tgtSnapName = 'va1'

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

Reply
0 Kudos