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
-Requestorparameter and the current date. By default, the requestor is you (
$env:USERNAMEvariable). 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
$env:USERNAME ($global:DefaultVIServer).User ($global:DefaultVIServers).User
- All you need is to pass VM object(s) to the
New-SmartSnapshotfunction in the pipeline.
Get-VM vm1 | New-SmartSnapshot
- Note the
Snapshot(snapshot name) and
Descriptionproperties of the returned object.
By default, the
New-SmartSnapshot prohibits multiple snapshots for a single VM, but it can be simply changed by using
Get-VM vm1 | New-SmartSnapshot -Description Patch1 Get-VM vm1 | New-SmartSnapshot -Description Patch2 -Verbose Get-VM vm1 | New-SmartSnapshot -Description Patch2 -Force
- 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
Connect-VIServer VC2 -wa SilentlyContinue Set-PowerCLiTitle
- The Set-PowerCLiTitle function is from the
Uidproperty 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
- Here we see that every of these two VM is managed by different VCenter. The
New-SmartSnapshotalso noticed this and have generated the
Descriptionaccording to this.
Eject VM CD-ROM drive
- By default the
New-SmartSnapshotejects VM CD-ROM before taking a snapshot, this behavior can be controlled by using
-EjectCDDriveparameter. 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
- Taking snapshot procedure runs as a task and visualized by the
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 . To fix it you will have to manually exclude VM memory from the snapshot.
All this dirty work the
New-SmartSnapshotwill do for you. Instead of fail you will get snapshot with no VM memory.
New-SmartSnapshotcan work in read only mode with
-ReportOnlyparameter. Retrieve existing snapshots and do nothing.
Get-VM | New-SmartSnapshot -ReportOnly Get-VM vm1, vm2 | snap -ReportOnly | ft –au
- 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