PowerCLI

The Way To PowerCLI : Create ,Delete and consolidate Snapshots

Virtual machine snapshots are useful , since they  preserve the state of a virtual machine’s virtual disk and, optionally, virtual memory before upgrading an application for example. VM Snapshots are also taken by a lot of backup applications for VMs at the start of the backup and then removed at the end of the backup like “EMC Avamar”.

Creating snapshots

we need to be connected to vCenter first bfore we start , to create snapshot use the following cmdlet “NewSnapshot”

New-Snapshot -vm test01 -Name b4Update

or you can use pipeline

get-vm -Name test01 | New-Snapshot -Name b4applypatch

2017-11-05_11-06-49

2017-11-05_11-07-07

Getting snapshots
Get-VM -name test01 | Get-Snapshot | Select VM,Name,Created,SizeMB | FT
2017-11-05_11-22-58
what if we want to get all snapshots which created older than 10 day’s for example
Get-VM| Get-Snapshot |Where {$_.Created -lt (Get-Date).AddDays(-10)}| Select-Object VM,Name,Created,SizeGB | FT
Deleting the snapshots

if you want to delete all Virtual Machine SnapShots

Get-VM -Name test01 | Get-Snapshot | Remove-Snapshot

You get a prompt for confirmation to delete all snapshots
2017-11-05_11-40-02

if you want to delete one snapshot , then use the name of the snapshot you want to delete

Get-VM -Name test01 | Get-Snapshot -name b4applypatch | Remove-Snapshot

2017-11-05_12-32-17

if you want to delete all snapshot older than 10 day’s

Get-VM -Name test01 | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-10)} | Remove-Snapshot
Consolidation

When you perform a Delete or DeleteAll operation on snapshots, the snapshot is immediately deleted from Snapshot Manager, after that the  .vmdk files are consolidated on-disk. If the consolidation fails, it happens that the (snapshot) vmdk’s may remain on disk , you can slove that you can use the Consolidation Cmdlet :

we need to check which Virtual Machine needs Consolidation

Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded}

after that we can Consolidate all Virtual Machine that need that

 Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} | foreach {$_.ExtensionData.ConsolidateVMDisks_Task()}
For more information about Snapshot check below links :
link 1 , link 2

” The way to VMware PowerCLI” Series Posts :

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