Managing Snapshots with PowerCLI
PowerCLI is a great tool to manage resources in VMware vCenter. It would be even better if it was a "real" CLI tool, instead of a Powershell module. I often use it for creating, restoring or deleting snapshots on multiple machines, usually when dealing with a cluster of some sort.
Spinning up PowerCLI¶
First things first, let's get a Powershell environment. We all know that you can install Powershell on Linux by now, but I'm definitely not soiling my Fedora with Powershell. Luckily the PowerCLI team has made an official PowerCLI container with all included.
Below are the commands for running the container with Docker or Podman, for which ever you are using. It will create the volume powercli which will store all files in the containers /root directory. That way you keep your shell history and scripts you save to the root directory.
Connecting to vCenter¶
Connect to the vCenter.
Tip
If you are using a selfsigned certificate, you need to run below command to be able to connect.
Creating Snapshots¶
Create an array with the VM objects you want to work with.
Create a snapshot of the VMs with the name before-upgrade.
Listing Snapshots¶
List snapshots.
List only the latests snapshots created.
Restoring Snapshots¶
Below command will restore the snapshot before-upgrade on each VM in the $VMs array.
ForEach($vm in $VMs) {Get-Snapshot -VM $vm | where {$_.name -eq "before-upgrade"} | Foreach-Object { Set-VM -VM $vm -SnapShot $_ }}
Restore det latest snapshot created.
ForEach($vm in $VMs) {Get-Snapshot -VM $vm | where {$_.IsCurrent -eq $true} | Foreach-Object { Set-VM -VM $vm -SnapShot $_ -Confirm:$false}}
Deleting Snapshots¶
Below command will delete the snapshot before-upgrade on all VMs in the $VMs array.
Or delete the latest one.