Cleaning up snapshots (inc. failed VCBs) on VC hosted on 2008 R2

Cleaning up snapshots (inc. failed VCBs) on VC hosted on 2008 R2

Hi,

I recently upgraded my VirtualCenter server to 4.0.1 running on 2008 R2. I’m very happy with it, except the old way of running scheduled powershell tasks in 2003, no longer works. This is my script to delete all snapshots older than 7 days:

Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer VCservername -User UserName -Password Password

Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-7)} | remove-snapshot -confirm:$false

The way to run this script (assuming it’s called Delete-Snapshots.ps1 and sits in C:\Windows\Scripts) is to go into the Actions tab of Task Scheduler and change the program settings to:

  • Program/script: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
  • Add arguments (optional): -noninteractive -nologo c:\windows\scripts\delete-snapshots.ps1

Alternatively, here is the xml file dump you can import into Task Scheduler (delete-snapshots.xml) – obviously you’ll need to change usernames to run the service as.

Ta,

Leo

Tags: