• Skip to primary navigation
  • Skip to main content

jdgreen.io

The personal blog of James Green

  • Home
  • Technology
    • Announcements
    • Industry
    • Reviews/Guides
    • Tech Field Day
    • Interviews
  • Health
    • Food
    • Biohacking
    • Fitness
  • Faith
  • Book Notes
    • Business Books
    • Faith Books
    • Health Books
    • Technology Books
  • About
  • Show Search
Hide Search

Banish Lingering Snapshots

James Green · Jan 7, 2015 ·

I regularly have customers who have problems with staff creating snapshots and forgetting to clean them up. This can lead to any number of issues, especially snapshots growing out of control and filling up a datastore. To help prevent this from happening, I’ve started creating a scheduled task (with their blessing, of course) to delete snapshots older than an agreed upon amount of days. You could approach this in lots of ways, but due to my love of PowerCLI, I decided to go that route. If you have this problem in your environment, check out the below snippet to clean it up!

So that this can be run as a Scheduled Task, you’ll need to provide credentials ahead of time for the Connect-VIServer operation. Use the New-VICredentialStoreItem cmdlet to create the credentials ahead of time, and then modify the path in the script to point to them.

One last thing – for tips on how to run a PowerCLI script as a scheduled task, check out this post from @alanrenouf — Running a PowerCLI Scheduled Task

 

[code language=”ps”] # How many days should we keep snapshots?
$howLong = 2

#—————————————–#
$creds = Get-VICredentialStoreItem -Host "[vCenter FQDN]" -File "D:Scriptscreds.xml"
Connect-VIServer [vCenter FQDN] -User $creds.user -Password $creds.password
$age = (Get-Date).AddDays(-$howLong)
Get-VM | Get-Snapshot | Foreach {
if($_.Created -lt $age){
Remove-Snapshot $_ -Confirm:$false
}
}
[/code]

VMware

James Green is an enterprise IT consultant, a product of an amazing IT community, and a partner in ActualTech Media. He is a serial vExpert designee and a passionate Tech Field Day delegate and supporter. » Read Full Bio...

James Green: View My Blog Posts

Copyright © 2023 · Monochrome Pro on Genesis Framework · WordPress · Log in

Posting....