Auto creation and deletion of Snapshots in Vsphere Environment using Powercli

Today we are gonna develop a script that will automatically delete multiple snapshots that has been taken in VMs running in the Vsphere environment. We use powercli to create the script and run over the vcenter where the VMs are hosted

Before we go ahead, I would like to share you a small brief on what is powercli, powercli is a command-line interface (CLI) tool for automating vSphere and vCloud management. VMware vSphere PowerCLI debuted as the VMware Infrastructure Toolkit, also called the VI Toolkit.

We have created two machine named test-vigneshbabu, test1-vigneshbabu as shown below:




Now we are gonna trigger a script that will take the snapshot of these machines as “before patching” as shown below, You can customise the name of the snapshots according to your standards.

Code :
$servers = Get-Content "c:\vm-serverslist.txt"
foreach ( $i in $servers )
{
 New-Snapshot -VM $i -Name before_pathing
}

Note: path of the get-content is customisable and it’s the file containing the list of machines where the snapshot has to be created.

After execution:


Now we have successfully created a script where we can create multiple snapshots provided we have the list of machines in txt file as shown above.

Just to ensure the snapshots are created we checked in the GUI and we have got the below output under the manage snapshots page



As we have created snapshots now its time we need to delete the snapshots which we have taken for two machines through powercli. As shown below type the commands



Code:

$servers = Get-Content "c:\vm-serverslist.txt"
foreach ( $i in $servers )
{
 Get-VM -Name $i | Get-snapshot | remove-snapshot -confirm:$false
}

Note: path of the get-content is customisable and it’s the file containing the list of machines where the snapshot has to be deleted.


After the execution of the above script the snapshots created for two machines will be deleted. Through powercli you can see the progress bar of the machine deletion status and it requires not human intervention for deletion or multiple popup boxes to confirm the deletion process.



In the above screenshot it clearly states that the snapshots has been deleted and this was taken from the vcenter console.

This small lines of scripts can be runned for n Number of VMs running over the Vcenter. This could save your hours of time which you can spend and concentrate on some other productive works. Voila and here you go.

Comments are welcome which can be enhanced over the above scripts.
  

Popular posts from this blog

How to reset the IE settings Through command Line (CMD)

Running CLI Commands in ESXi using plink software