Creating VMware Snapshots with PowerCLI

This is - Getting started with PowerShell and VMware vSphere - Part 1



Creating a snapshot, is a trivial and needed task most of the time. It is a very good idea to have it prior some changes in a virtual host.

    To create it, the virtual machine can be on or off. Also the snapshot will be created using that state. This might be important because if you roll back a running virtual machine to a snapshot where the virtual machine was stopped, the virtual machine will be stopped.     You will use the New-Snapshot cmdlet.

    When we create the snapshot you will need to give it a name... Also a good idea to provide a description. If the virtual machine is running, we can also elect to save the memory state as well with the –Memory parameter. Moreover, if the it has virtual tools installed, we can opt for a quiescent snapshot with –Quiesce

    This has the effect of saving the virtual disk in a consistent state. That is, the snapshot occurs without the operating system in the middle of trying to write something to disk. Personally, I remember to have less problems restoring a snapshot with –Quiesce, although there is a slight trade-off....  time it takes to create the snapshot. In fact, because a snapshot can take a few minutes to create, you may want to create it in the background on the VMware host using –RunAsycn.

2 ways I have tested:

  • New-Snapshot -VM MACHINE-NAME -Name 'NAME OF SNAP' -Description 'This is a test powershell snapshot' -Quiesce -Memory
  • Get-Vm MACHINE-NAME | New-Snapshot -Name "NAME OF SNAP" -Description 'This is a test powershell snapshot' -Quiesce -Memory


    Now, the beauty of scripting.... Naturally, if we can do it for one VM, we can do it for many :-)

Get-Vm MACHI* | new-snapshot -name "NAME OF SNAP" -Description "Created $(Get-Date) powershell" -Quiesce -Memory –RunAsync

    This will trigger a snapshot of all virtual machines which have names starting with MACHI*

Listing Snapshots

    To enumerate snapshots, just use Get-Snapshot. The snapshots are associated with the virtual machine, so you’ll need to specify one or we can use wildcards:

Get-Snapshot -vm MACHINE-NAME


Now that we have names, we can also get relevant information about the snap:

 Get-Snapshot -vm MACHINE-NAME -name SNAP-NAME | select *


Restoring from a Snapshot

To restore, we will use the Set-VM cmdlet. 
1st thing to do, assing the snap you want to a variable:

$snap = Get-Snapshot -vm MACHINE-NAME -name SNAP-NAME

Now we can easily use Set-VM to revert:

set-vm -VM MACHINE-NAME -Snapshot $snap -whatif




*Use -whatif to discover guess what? "What if" you ran that command what actions will take course. 

Removing Snapshots 

    Periodically you may want to clean up old snapshots using Remove-Snapshot. The easiest way is to get the snapshot or snapshots and pipe them to Remove-Snapshot. Some snapshots may have child snapshots so use –RemoveChildren to clean as well.

Get-Snapshot -name SNAP-NAME -VM MACHINE-NAME | remove-snapshot


    Now, imagine, we have multiple virtual machines with snapshots on that use the same name. We can use wildcards of course... be aware that this action would take some time to complete.... so let's running it asynchronously :-)

Get-Snapshot -name SNAP-NAME -vm * | remove-snapshot -RemoveChildren -RunAsync





If you lost howto install click here



Next topic will be New-VM.


=======================================================================

"Do you already know my Linux training !?" Available in portuguese only.


"Você já conhece meu treimanento de linux!?"

Neste treinamento vamos abordar de forma simples, rápida e sem “enrolação”  como instalar o Linux CentOS 8 utilizando o VirtualBox e a nuvem da Amazon AWS do zero.



Nenhum comentário:

Postar um comentário