VMware Cloud Community
VSnip
Enthusiast
Enthusiast
Jump to solution

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

Hi guys,

I don't find the solution :smileycry:

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!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

}

-------------------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

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

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VSnip
Enthusiast
Enthusiast
Jump to solution

Thanks a lot LucD​ !!!!

It works perfectly!

0 Kudos
VSnip
Enthusiast
Enthusiast
Jump to solution

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 :smileycry:

Can you help me? please

Thanks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

}

-------------------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
VSnip
Enthusiast
Enthusiast
Jump to solution

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!!! :smileycool:

0 Kudos