Home VMwareAutomation VM Snapshot via PowerCLI in vCenter

VM Snapshot via PowerCLI in vCenter

by Mohamed Imthiyaz
snapshot

In virtualized environments like VMware, snapshots play a crucial role in managing the state of virtual machines (VMs). A snapshot is a point-in-time copy of a VM’s disk and memory state, allowing administrators to preserve the VM’s configuration and data at a specific moment. I aim to explain the snapshot process in this blog, highlighting its benefits and considerations.

What is a Snapshot?

A snapshot is a copy of a VM’s disk and memory state at a specific time. When a snapshot is taken, it captures the current state of the VM, including its memory, settings, and disk contents. Subsequent changes made to the VM after taking the snapshot are recorded in a separate file known as a delta file or snapshot disk.

Benefits of Snapshots:

Backup and Recovery: Snapshots provide a convenient way to create backups of VMs, enabling easy restoration to a specific state if needed.

Testing and Development: Snapshots allow administrators to test software updates or configurations without affecting the production environment. If something goes wrong, they can revert the VM to the pre-snapshot state.

PowerCLI Script

The Below PowerCLI script automates VM snapshot creation and tracks the task time. It connects to vCenter, takes a snapshot of the specified VM, and records the time taken for the task. Useful for backup, testing, and VM state preservation.

Connect-VIServer -Server <vCenter_Server_Name> -User <Username> -Password <Password>

$vmName = "Your VM Name"
$vm = Get-VM -Name $vmName

Write-Host "Taking snapshot for VM: $vmName"
$startTime = Get-Date
$vm | New-Snapshot -Name "SnapshotName" -Description "Snapshot Description" -Quiesce -Memory
Write-Host "Snapshot creation initiated."

# Wait for snapshot creation to complete
while ($vm.ExtensionData.Runtime.PowerState -ne 'PoweredOn') {
    Start-Sleep -Seconds 10
}

$endTime = Get-Date
$snapshotTime = $endTime - $startTime

Write-Host "Snapshot for VM '$vmName' created in: $($snapshotTime.Hours)h $($snapshotTime.Minutes)m $($snapshotTime.Seconds)s."

# Disconnect from the vCenter server
Disconnect-VIServer -Server <vCenter_Server_Name>  -Confirm:$false

Get all VM snapshots older than X days

In my case, I am pulling all the VM Snapshots which are older than 5 Days

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-5)} | Select-Object VM, Name, Created

Delete Snapshots older than X days

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-5)} | Remove-Snapshot -Confirm:$false

From vCenter

To take the Snapshot from UI, you can log in to the vCenter

Right on the VM -> Snapshots -> Take Snapshot

You can include the VM memory if you want, it will take a longer time to create the snapshot.

Snapshot

For more information about it, please check VMware’s kb. If you want to filter the Events from vCenter via PowerCLI you can check Get Custom Fields from vCenter Events via PowerCLI.

Hope this helps.

You may also like

Leave a Comment