VMware Cloud Community
Fred_Weston
Contributor
Contributor

How to use PowerCLI to automate snapshot creation / deletion

I am trying to use PowerCLI to automate snapshot creation and deletion so I have a quick & dirty safety net to protect against user error.

Using this command I am able to create a snapshot for all VMs:

get-vm | new-snapshot -Name "DailySnap" -Description "Daily Snapshot" -Quiesce -Memory

One problem I have with this is that even though I'm not using the -RunAsync switch, a bunch of snapshots all try to process at the same time, which isn't what I want.  I want one snapshot to run at a time so I don't kill the storage performance.

Also, ideally I would like to have more than one snapshot, i.e. one snapshot per day for the past 5 days, and continually delete the oldest snapshot.  So for example on days 1-5 a snapshot gets created and on day 6 a new snapshot gets created and snapshot 1 gets deleted, on day 7 a new snapshot gets created and snapshot 2 gets deleted, etc.  The problem is that subsequent snapshots seem to be treated as children of any existing snapshots, like this:


VM

-Snap1

--Snap2

---Snap3

----Snap4


Is it possible to prevent new snapshots from being children so it would look like this?


VM

-Snap1

-Snap2

-Snap3

-Snap4

Tags (1)
0 Kudos
5 Replies
sneddo
Hot Shot
Hot Shot

OK, I guess there are three parts to this:

1. Unless you do a revert operation after taking the snapshot, of course subsequent snapshots are going to be children of the current snapshot. All in all, not a big deal.

2. You may need to use Get-Task to determine if there is currently a snapshot task (or tasks). By default vCenter will run a certain number of concurrent operations which you probably don't want to mess with too much.

3. Should be simple enough to do a Get-Snapshot <<vm>>| Sort-Object Created | Select -First 1 to return the oldest snapshot and pipe it to Remove-Snapshot

0 Kudos
Fred_Weston
Contributor
Contributor

I guess I was operating under the impression that snapshot operations would occur one at a time (waiting for one to finish before beginning the next) unless I explicitly tell it I don't want to wait for completion using the -RunAsync switch.  Apparently that is not the case.  Is there some sort of switch I can specify to indicate that I want execution to stop until each task completes?

3. Should be simple enough to do a Get-Snapshot <<vm>>| Sort-Object Created | Select -First 1 to return the oldest snapshot and pipe it to Remove-Snapshot

Sure, but I assumed that if I have a snapshot hierarchy that looks like this:

Snap1

-Snap2

--Snap3

---Snap4

and I remove Snap1 (which is the base snapshot) then that would invalidate snapshots 2-4 as well since those are child snapshots.  Are you saying that if I remove snap1 then snap2 will become the new base and the tree will look like this?

Snap2

-Snap3

--Snap4

I just tried doing that manually and it appears to be the case, so that's good.

0 Kudos
sneddo
Hot Shot
Hot Shot

I guess I was operating under the impression that snapshot operations would occur one at a time (waiting for one to finish before beginning the next) unless I explicitly tell it I don't want to wait for completion using the -RunAsync switch.  Apparently that is not the case.  Is there some sort of switch I can specify to indicate that I want execution to stop until each task completes?

Nope, the snapshot operations will basically just create a vCenter task and will run x number of tasks concurrently (forget the exact number, maybe 30?). You can limit the number, but this will effect all vCenter tasks, not just snapshots. If you want to limit just snapshots it will be better to impose the limit in script. Just have a while loop based on running snapshot tasks.

Are you saying that if I remove snap1 then snap2 will become the new base and the tree will look like this?

Correct Smiley Happy

0 Kudos
Fred_Weston
Contributor
Contributor

So...what is the function of the -RunAsync switch?

Any chance you can point me to an example of using a while loop to do this?  I'm not too up on powershell or powercli.

0 Kudos
sneddo
Hot Shot
Hot Shot

So...what is the function of the -RunAsync switch?

It's a little confusing. Basically it means that if it is specified, the New-Snapshot command will continue immediately instead of waiting. So it applies to the cmdlet rather than the snapshot operation, if that makes sense?

Any chance you can point me to an example of using a while loop to do this?  I'm not too up on powershell or powercli.

Sure. I haven't had a chance to test this, but something like this should get you started:

$concurrentSnaps = 1

$VMs = Get-VM

for ($i = 0; $i -lt $VMs.count; $i++)

{

   while (@(Get-Task | Where {$_.Description -match "Create Snapshot" }).count -ge $concurrentSnaps)

   {

      Sleep -Seconds 10

   }

   $VMs[$i] | New-Snapshot -Name "DailySnap" -Description "Daily Snapshot" -Quiesce -Memory

}

0 Kudos