Snapshot List of VM’s from csv – PowerCLI

Many may have come across a situation where you need to take snapshot on a set a VMs for maintenance activities. In an environment having 1500+ machines, this can be tedious.The below command conmes in handy for such tasks.

To snapshot a VM named DSL1, use below command line

Get-VM DSL1 | New-Snapshot -Name "Patching" -Description "Before Patching" -Quiesce:$false -Confirm:$false

To remove the snapshot, use below

Get-VM DSL1 | Get-Snapshot | Remove-Snapshot -RemoveChildren -confirm:$false

Now, to snapshot more number of VMs, the above method will be time consuming, hence we export the VM list that needs to be snapshoted into a csv and make use of it.1

 

Here i have VMs named DSL1-6 that needs a snapshot, I saved them in a VMList.csv and placed the file in D drive.

Now we can modify the previous command to Import VM names from the csv and perform snapshot on them.

Import-Csv "D:\VMList.csv" | %{Get-VM $_.Name | New-Snapshot -Name "Patching" -Description "Before Patching" -Quiesce:$false -Confirm:$false}

2

To remove the snapshots all at once, use

Import-Csv "D:\VMList.csv" | %{Get-Snapshot $_.Name | Remove-Snapshot -RemoveChildren -Confirm:$false}

If you observe the output closely, the PowerState of the Snapshot is PoweredOff, meaning the VM will be in poweredoff state if at all you revert the snapshot. You can also revert to snapshot on all VMs at once, we just need to tweak the command and use the “Name” of the snapshot to which we want to revert.

Import-Csv "D:\VMList.csv" | %{Set-VM $_.Name -Snapshot "Patching"  -Confirm:$false}

Since the VMs will be  in powered off state after reverting, we need to power on them

Import-Csv "D:\VMList.csv" | %{Start-VM $_.Name -Confirm:$false}

You can perform any number of operations like this just by tweaking the command line.

Hope this was informative. Thanks!

Advertisements