5 Replies Latest reply on Aug 19, 2019 7:55 AM by VSnip

    Remove Snapshot and specific tag / Add another Tag on the same command line

    VSnip Novice

      Hi guys,

       

      I don't find the solution

      I hope you could help me!

       

      I tried these commands :

      $myTag = Get-Tag "Snap_3d"
      Get-VM -Tag "Snap_15d" | Get-Snapshot | Remove-Snapshot -Confirm:$False | Get-TagAssignment -Category "Snapshot" 
      | Remove-TagAssignment -Confirm:$false 
      | New-TagAssignment -Tag $myTagmyTag
      

       

      The goal is:

      - Remove snapshots for VMs with "Snap_15d"

      - Once it's done, remove this Tag

      - Assign Snap_3d tag

       

      => I'd like to perform all this actions on the same command line (Is it possible?)

       

      Snap_3d & Snap_15d are in Category Tag: Snapshot

       

      Thanks for your help!

        • 1. Re: Remove Snapshot and specific tag / Add another Tag on the same command line
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Try like this

           

          Get-VM -Tag Snap_3d |

          ForEach-Object  -Process {

             Get-Snapshot -VM $_ | Remove-Snapshot -Confirm:$false

             Get-TagAssignment -Entity $_ -Category Snapshot | where{$_.Tag.Name -eq 'Snap_3d'} | Remove-TagAssignment -Confirm:$false

             New-TagAssignment -Entity $_ -Tag Snap_15d

          }

          • 2. Re: Remove Snapshot and specific tag / Add another Tag on the same command line
            VSnip Novice

            Thanks a lot LucD !!!!

             

            It works perfectly!

            • 3. Re: Remove Snapshot and specific tag / Add another Tag on the same command line
              VSnip Novice

              Hi LucD,

               

              After some tests and vacations... something goes wrong with the script.

              The script filters on the tag then these actions are executed:

              - Delete Snapshot older than 15 days

              - Delete TagX

              - Add TagY

              (Here VM will be automatically tagged "TagY" even if Snapshot is not older than 15 days)

               

              But I would like this:

              If VM is tagged with "TagX" and snapshot is older than "X days"

              then we execute these actions:

              - Remove Snapshot

              - Delete the TagX on the VM

              - Add the TagY on the VM

               

              I tried this:

               

              Get-VM -Tag Snap_15d | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-14)} | 
              ForEach-Object  -Process {
              
              
                 Remove-Snapshot -Snapshot $_.Name -Confirm:$false
                 Get-TagAssignment -Entity $_.VM -Category Snapshot | where{$_.Tag.Name -eq 'Snap_15d'} | Remove-TagAssignment -Confirm:$false
                 New-TagAssignment -Entity $_.VM -Tag Snap_3d
              }
              

               

              I had this error:

              Remove-Snapshot : Cannot bind parameter 'Snapshot'. Cannot convert the "INP1421B - 08%252f05%252f2019 06:01:11" value of type "System.String" to type 

              "VMware.VimAutomation.ViCore.Types.V1.VM.Snapshot".                                                                                                   

              At D:\***********\test-snap15.ps1:27 char:30                                                                              

              +    Remove-Snapshot -Snapshot $_.Name -Confirm:$false                                                                                                

              +                              ~~~~~~~                                                                                                                

                  + CategoryInfo          : InvalidArgument: (:) [Remove-Snapshot], ParameterBindingException                                                       

                  + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveSnapshot                              

              I can't find the solution

               

              Can you help me? please

               

              Thanks.

              • 4. Re: Remove Snapshot and specific tag / Add another Tag on the same command line
                LucD Guru
                User ModeratorsCommunity WarriorsvExpert

                Try something like this

                 

                Get-VM -Tag Snap_3d |

                 

                Get-Snapshot -VM $_ |

                where { (New-TimeSpan -Start $_.Created -End (Get-Date)).TotalDays -ge 15 } |

                ForEach-Object -Process {

                   Remove-Snapshot -Snapshot $_ -Confirm:$false

                   Get-TagAssignment -Entity $_.VM -Category Snapshot |

                     where { $_.Tag.Name -eq 'Snap_3d' } |

                     Remove-TagAssignment -Confirm:$false

                   New-TagAssignment -Entity $_ -Tag Snap_15d

                }

                • 5. Re: Remove Snapshot and specific tag / Add another Tag on the same command line
                  VSnip Novice

                  Thanks LucD !!!

                   

                  My final script is:

                  Get-VM -Tag Snap_15d | Get-Snapshot | where { (New-TimeSpan -Start $_.Created -End (Get-Date)).TotalDays -ge 16 } |

                  ForEach-Object -Process {

                     Remove-Snapshot -Snapshot $_ -Confirm:$false

                     Get-TagAssignment -Entity $_.VM -Category Snapshot | where { $_.Tag.Name -eq 'Snap_15d' } | Remove-TagAssignment -Confirm:$false

                     New-TagAssignment -Entity $_.VM -Tag Snap_3d

                  }

                  It works!

                   

                  You rock!!!