8 Replies Latest reply on Sep 22, 2020 11:37 AM by LucD

    Get-snapshot and the condition if

    IKirill Enthusiast

      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.

        • 1. Re: Get-snapshot and the condition if
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Get-snapshot and the condition if
            IKirill Enthusiast

            Thanks LUCD!

            • 3. Re: Get-snapshot and the condition if
              IKirill Enthusiast

              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)

              • 4. Re: Get-snapshot and the condition if
                LucD Guru
                Community WarriorsvExpertUser Moderators

                Did you assign a value to the variable $snapName?


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

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: Get-snapshot and the condition if
                  IKirill Enthusiast

                  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
                              }
                  }
                  
                  • 6. Re: Get-snapshot and the condition if
                    LucD Guru
                    User ModeratorsCommunity WarriorsvExpert

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

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    • 7. Re: Get-snapshot and the condition if
                      IKirill Enthusiast

                      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 
                      } 
                      }
                      
                      • 8. Re: Get-snapshot and the condition if
                        LucD Guru
                        Community WarriorsvExpertUser Moderators

                        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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz