Automation

 View Only
  • 1.  Get-snapshot and the condition if

    Posted Sep 14, 2020 04:57 PM

    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.



  • 2.  RE: Get-snapshot and the condition if
    Best Answer

    Posted Sep 14, 2020 05:19 PM

    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

        }

    }



  • 3.  RE: Get-snapshot and the condition if

    Posted Sep 14, 2020 05:25 PM

    Thanks LUCD!



  • 4.  RE: Get-snapshot and the condition if

    Posted Sep 22, 2020 05:17 PM

    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)



  • 5.  RE: Get-snapshot and the condition if

    Posted Sep 22, 2020 05:20 PM

    Did you assign a value to the variable $snapName?


    You could do a Read-Host to let the operator assign a value.



  • 6.  RE: Get-snapshot and the condition if

    Posted Sep 22, 2020 05:33 PM

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



  • 7.  RE: Get-snapshot and the condition if

    Posted Sep 22, 2020 05:48 PM

    You don't have a pipeline symbol (|) at the end of the line with Group-Object.



  • 8.  RE: Get-snapshot and the condition if

    Posted Sep 22, 2020 05:57 PM

    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

    }

    }



  • 9.  RE: Get-snapshot and the condition if

    Posted Sep 22, 2020 06:37 PM

    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

            }

        }

    }