Intellectual VMware snapshots

Cover

Today we will automate VMware VM snapshots with a New-SmartSnapshot function from my PowerCLi Vi-Module module. The function will utilize the embedded PowerCLi New-Snapshot cmdlet but with some smart features.

New-SmartSnapshot – the smart features

A snapshot name and description

  • The function generates the snapshot name from -Requestor parameter and the current date. By default, the requestor is you ($env:USERNAME variable). This is user account, used to open the PowerShell console.

  • The snapshot description is constructed from account name, used to connect to VCenter plus optional -Description parameter.

$env:USERNAME
($global:DefaultVIServer).User
($global:DefaultVIServers).User
  • All you need is to pass VM object(s) to the New-SmartSnapshot function in the pipeline.
Get-VM vm1 | New-SmartSnapshot
  • Note the Snapshot (snapshot name) and Description properties of the returned object.

01.New-SmartSnapshot

Multiple snapshots

By default, the New-SmartSnapshot prohibits multiple snapshots for a single VM, but it can be simply changed by using -Force parameter.

Get-VM vm1 | New-SmartSnapshot -Description Patch1
Get-VM vm1 | New-SmartSnapshot -Description Patch2 -Verbose
Get-VM vm1 | New-SmartSnapshot -Description Patch2 -Force

02.New-SmartSnapshot_Params

  • If you want to prohibit taking snapshots at all or set maximum allowed snapshot number for any VM(s), please use my Set-MaxSnapshotNumber function.

Multiple VC support

  • Let’s remove all snapshots for our VM and connect to a second VC.
Get-VM lab1vm1 | Get-Snapshot | Format-Table -AutoSize
Get-VM lab1vm1 | Get-Snapshot | Remove-Snapshot –RunAsync
Get-VM lab1vm1 | Get-Snapshot | Format-Table –AutoSize

03.New-SmartSnapshot_RemoveSnap

Connect-VIServer VC2 -wa SilentlyContinue
Set-PowerCLiTitle
  • The Set-PowerCLiTitle function is from the Vi-Module too.

  • The Uid property of any VMware object will give us the server through which we are managing that object in the current session.

Get-VM vm1, vm2 | select Name, Uid

04.New-SmartSnapshot_MultipleVC

  • Here we see that every of these two VM is managed by different VCenter. The New-SmartSnapshot also noticed this and have generated the Description according to this.

Eject VM CD-ROM drive

  • By default the New-SmartSnapshot ejects VM CD-ROM before taking a snapshot, this behavior can be controlled by using -EjectCDDrive parameter. An ISO image, mounted to VM CD Drive as part of VM snapshot will prevent you from deleting the ISO or Datastore itself in the future. This kind of lock is very hard to determine.
Get-VM vm1, vm2 | New-SmartSnapshot -EjectCDDrive:$false

Snapshot progress

  • Taking snapshot procedure runs as a task and visualized by the Write-Progress.

05.New-SmartSnapshot_Progress

Snapshot VM memory

  • One of cool features of the snapshot technology is ability to save VM memory as part of the snapshot. But if you will try to snapshot powered on VM which has independent disks, the snapshot will fail Emoj. To fix it you will have to manually exclude VM memory from the snapshot.

  • All this dirty work the New-SmartSnapshot will do for you. Instead of fail you will get snapshot with no VM memory.

Snapshot report

  • The New-SmartSnapshot can work in read only mode with -ReportOnly parameter. Retrieve existing snapshots and do nothing.

Get-VM | New-SmartSnapshot -ReportOnly
Get-VM vm1, vm2 | snap -ReportOnly | ft –au

06.New-SmartSnapshot_Report

Summary

  • For more details about the function, please take a look at the content based help and examples.
Get-Alias –Definition New-SmartSnapshot
Get-Help New-SmartSnapshot -Full
Get-Help New-SmartSnapshot -Examples
Get-Help New-SmartSnapshot -Parameter EjectCDDrive

You might also like

Set-PowerCLiTitle – Connect-VIServer deep dive
Set-MaxSnapshotNumber – Control maximum number of VMware snapshots
Copy VMware VM Notes to Computer/AD Computer Description
Expand-VMGuestPartition – Extend VM Guest Partition w/PowerCLi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s