VMware Cloud Community
jssidhu71
Contributor
Contributor
Jump to solution

Using PowerCLI to take snapshots and then removing snapshots

Hi - Wonder if someone has a script to do the following:

Scenerio:

I have 400 Virtual Desktops within a cluster, i need to create a snapshot at the beginning of the week for 100 VMs, after 2 weeks, the snapshots for these 100 VMS will be removed reverting back to the original image.

Can this be done..? if so, can anyone assist please.

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This should do the trick

Import-Csv "C:\VMnames.csv" | %{
	Get-VM $_.Name | New-Snapshot -Name "Weekly snapshots" -Quiesce:$true -Confirm:$false
}
$twoweeksago = (Get-Date).AddDays(-14)
Get-VM | Get-Snapshot | %{
	if($_.Created.CompareTo($twoweeksago) -eq -1){
		Remove-Snapshot $_ -Confirm:$false
	}
}

The CSV file should look like this

Name
vm1
vm2
...

I assumed that the snapshot removal had to run over all 400 VMs.

If not the 2nd lookup has to be moved inside the Import-Csv loop.

You can schedule the script with Windows Scheduler.

Have a look at Alan's Running a PowerCLI Scheduled task post for the details.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
43 Replies
LucD
Leadership
Leadership
Jump to solution

How would these 100 VMs be selected out of the 400 you have ?

And is this a process that should be done every week, let's say on Monday ?

And did I understand correcly that once a snapshot is 2 weeks old, it can be removed ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jssidhu71
Contributor
Contributor
Jump to solution

How would these 100 VMs be selected out of the 400 you have ?

--> A list would be supplied listing VM names

And is this a process that should be done every week, let's say on Monday ?

--> Yes, every other week

And did I understand correcly that once a snapshot is 2 weeks old, it can be removed ?

--> Yes, snapshot to be removed completely

0 Kudos
maishsk
Expert
Expert
Jump to solution

Throttling Snapshots

This can give you an idea of how to throttle the removal and how to create the snapshots


Maish - VCP - vExpert 2010

VMware Communities User Moderator

Virtualization Architect & Systems Administrator

Twitter - @maishsk

Maish Saidel-Keesing • @maishsk • http://technodrone.blogspot.com • VMTN Moderator • vExpert • Co-author of VMware vSphere Design
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This should do the trick

Import-Csv "C:\VMnames.csv" | %{
	Get-VM $_.Name | New-Snapshot -Name "Weekly snapshots" -Quiesce:$true -Confirm:$false
}
$twoweeksago = (Get-Date).AddDays(-14)
Get-VM | Get-Snapshot | %{
	if($_.Created.CompareTo($twoweeksago) -eq -1){
		Remove-Snapshot $_ -Confirm:$false
	}
}

The CSV file should look like this

Name
vm1
vm2
...

I assumed that the snapshot removal had to run over all 400 VMs.

If not the 2nd lookup has to be moved inside the Import-Csv loop.

You can schedule the script with Windows Scheduler.

Have a look at Alan's Running a PowerCLI Scheduled task post for the details.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jssidhu71
Contributor
Contributor
Jump to solution

Hey LucD.. many thanks for this..

how would the script look if i wanted to list a number of VMs to be snapshotted, then remove the snapshot back to the original image without the duration .. As i may have to snapshot a number of vms then remove the snapshot for the said vms..

Thanks Again !!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure I understood that last question.

Do you want to take a snapshot and then immediatly remove it ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jssidhu71
Contributor
Contributor
Jump to solution

Hey LucD..

I want to list a 100 VMS to create a snapshot leaving for a few days, i now want to list the same 100 VMS to remove the snapshot regardless of how long the snapshot has been taken.. basically snapshot a list of vms, then run a script listing vms to remove snapshot.

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's quite easy.

To create the snapshots

Import-Csv "C:\VMnames.csv" | %{
     Get-VM $_.Name | New-Snapshot -Name "MySnapshot" -Quiesce:$true -Confirm:$false
}

To remove the snapshots

Import-Csv "C:\VMnames.csv" | %{
   Get-VM $_.Name | Get-Snapshot -Name "MySnapshot" | Remove-Snapshot -Confirm:$false
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jssidhu71
Contributor
Contributor
Jump to solution

Many Thanks !!

0 Kudos
Parimal24
Contributor
Contributor
Jump to solution

Sorry to resurrect an old post. Thanks  LucD - with your script, I am able to take snapshots from CSV list perfectly, however, I am unable to commit/delete them when I run this code and get error:

Remove-Snapshot : Cannot bind parameter 'Snapshot'. Cannot convert the "@{Name=
SERVER1}" value of type "System.Management.Automation.PSCustomObject" to t
ype "VMware.VimAutomation.ViCore.Types.V1.VM.Snapshot".
At C:\Support\RemoveSnaps.ps1:2 char:96
+    Get-VM $_.Name | Get-Snapshot -Name "July Week 1 Patching CN000000099992"
| Remove-Snapshot <<<<  $_ -Confirm:$false
    + CategoryInfo          : InvalidArgument: (:) [Remove-Snapshot], Paramete
   rBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.RemoveSnapshot

Not sure what could be causing this? Can you decipher? I am running VCenter 4.1.0

0 Kudos
Parimal24
Contributor
Contributor
Jump to solution

Actually got it working by removing  $_ in the Remove-Snapshot cmdlet:

Import-Csv "C:\Support\Snapshotlist2.csv" | %{
   Get-VM $_.Name | Get-Snapshot -Name "July Week 1 Patching" | Remove-Snapshot -Confirm:$false
}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that was a typo. I corrected the code.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
orgazi
Contributor
Contributor
Jump to solution

Hi;

What if we put ip addresses of VMs instead of names of them into the csv file,then how would the script be changed?

Thank you.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It depends what the Displayname of the VMs are. Do they correspond with the hostname configured in the guest OS ?

If yes, the script would first need to read all the IP addresses from the CSV file.

Then do a name resolution, which would give the FQDN of the guest.

The first qualifier of the FQDN should be the same as the Displayname of the VM.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
orgazi
Contributor
Contributor
Jump to solution

Unfortunately display names and FQDN are not match in our env.

If it is better,we can put FQDNs in the csv file.

Actually the story is this:

Windows team gave me the list of vms' IPs and FQDNs,and want me to take snapshot them before patching.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ip or FQDN is not the issue, we can easily convert between the two from within a PowerShell script.

Are the VMware Tools installed in each VM ?

If yes, we can use the FQDN to find the VM.

Something like this

$vmTab = @{}

# Set up hash table: key = FQDN

Get-VM | %{

    $vmTab.Add($_.Guest.HostName,$_)

}

# Loop through CSV, lookup VM on FQDN

Import-Csv vm.csv -UseCulture | %{

        Get-Snapshot -VM $vmTab[$_.fqdn]

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
orgazi
Contributor
Contributor
Jump to solution

We got some error like:

Exception calling "Add" with "2" argument(s): "Key cannot be null.

Parameter name: key"

Our csv file goes like this:

test1,test2....

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Was that by any chance for a VM that didn't have the VMware Tools installed ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
orgazi
Contributor
Contributor
Jump to solution

Nope Smiley Sad

both have vmtools running.

0 Kudos