Lab Automation with PowerCLI Part 4: Working with Snapshots
Snapshots are easily one of the most important ideas associated with virtual machines. Being able to capture the state of a Machine and revert to a known good state saves time and money every day. VMware exposes the ability to manipulate Snapshots via PowerCLI.
This article aims to cover the following points:
- Create a snapshot
- Edit a snapshot (Name/Description)
- Set a VM to use a Snapshot
- Remove a snapshot
Notes:
- For reference: Part 1, Part 2, Part 3
- PowerCLI 5
- Powershell V2
- PowerCLI Cmdlet reference
- Example Scripts download (Adfly)
Create a snapshot:
#Add-PSSnapin "Vmware.VimAutomation.Core"
# Uncomment if the script doesn't work
#Connect to ESXi host, Create a snapshot, close connection
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password Something!!
New-Snapshot -VM TestVM2 -Name BaseSnapshot -Description "OS Installed, Network configured. Ready for Install"
Disconnect-VIServer -Server $ESXIHost -Confirm:$false
The New-Snapshot command allows you to take a VM and create a snapshot with both a name and a description. This script makes a snapshot named BaseSnapshot and gives it some descriptive text. Here is a pic showing the result looks like:
Edit a Snapshot's name or Description:
#Connect to ESXi host, Set a snapshot Name, close connection
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password Something!!
Set-Snapshot -Snapshot (Get-Snapshot -VM TestVM2 -Name BaseSnapshot) -Name "Base Snapshot (IP:10.19.3.45)"
Disconnect-VIServer -Server $ESXIHost -Confirm:$false
I don't think I'll be using the Set-Snapshot command very often in lab automation but wanted to touch on it here for completeness. It will let you take a snapshot and change its name and description.
You may have noted the nested Get-Snapshot command in the above script. Get-Snapshot returns a snapshot object which you can use here and in the sample scripts below. When working on scripts which touch a lot of virtual machines you may want to replace the nested Get-Snapshot command with a variable containing a Snapshot object. Here's a pic showing the resulting name change:
Set a Virtual Machine to use a Snapshot:
#Connect to ESXi host, Set a VM to use a particular snapshot, close connection
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password Something!!
Set-VM -VM TestVM2 -Snapshot (Get-Snapshot -VM TestVM2 -Name "Base Snapshot (IP:10.19.3.45)") -Confirm:$false
Disconnect-VIServer -Server $ESXIHost -Confirm:$false
Note the use of the nested Get-Snapshot to return a particular snapshot that applies to the VM. You can use Set-VM for more than just reverting / applying a particular snapshot. In this script we set Confirm:$false so we aren't prompted for confirmation.
Remove a snapshot from a Virtual Machine:
#Connect to ESXi host, Set a VM Snapshot, close connection
$ESXIHost = "10.10.10.100"
Connect-VIServer -Server $ESXIHost -User root -Password Something!!
Remove-Snapshot -Snapshot (Get-Snapshot -VM TestVM2 -Name "Base Snapshot (IP:10.19.3.45)") -Confirm:$false
Disconnect-VIServer -Server $ESXIHost -Confirm:$false
Remove-Snapshot is vey straight forward. No mystery about what it does. In this example I use Get-Snapshot to pull the snapshot I want to destroy and specify Confirm:$false so it doesn't prompt for removal.
Being able to work with snapshots through Powershell opens up some interesting possibilities for lab management and QA. With a little more scripting or coding you could setup a large-scale test environment which can be reset quickly and easily. If you dig deeper into VMWare integration you can use the VMWare tools and VIX for some serious automation!
The scripts listed above are available for download here (Adfly).