I have a need to create a snapshot of a VM (Esx vSphere 4.0 upd 1) everyday. I would like to keep two days of snapshots for these VMs. Meaning, a script that will delete/commit the snapshots that are older than two days. What can I do to accomodate this? Thanks
You could do something like this
If you schedule this script to run daily, it will
*) create a new snapshot
*) remove all snapshots in excess of $targetSnaps
$vmName = <vm-name> $targetSnaps = 2 $vm = Get-VM -Name $vmName # Create daily snapshot $vm | New-Snapshot -Name ($vmName + " " + (Get-Date).ToShortDateString()) # Remove excess snapshots $snaps = $vm | Get-Snapshot | Sort-Object -Property Created -Descending if($snaps.Count -gt $targetSnaps){ $targetSnaps..($snaps.Count - 1) | %{ $snaps[$_] | Remove-Snapshot -Confirm:$false } }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could do something like this
If you schedule this script to run daily, it will
*) create a new snapshot
*) remove all snapshots in excess of $targetSnaps
$vmName = <vm-name> $targetSnaps = 2 $vm = Get-VM -Name $vmName # Create daily snapshot $vm | New-Snapshot -Name ($vmName + " " + (Get-Date).ToShortDateString()) # Remove excess snapshots $snaps = $vm | Get-Snapshot | Sort-Object -Property Created -Descending if($snaps.Count -gt $targetSnaps){ $targetSnaps..($snaps.Count - 1) | %{ $snaps[$_] | Remove-Snapshot -Confirm:$false } }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So you would need to run a powershell by using scheduled tasks on a daily basis to have the snapshot taken, then you can either run the clean up in the same script or in a separate scripts.
To create the snapshot,
New-Snapshot -VM (get-vm vmname) -Name nameyouchoose
To delete the snapshot based on your criteria of 2 days,
$OldSnapshot = Get-VM vmname | get-snapshot | where {$_.Created -lt ((Get-Date).AddDays(-2))} Remove-Snapshot -Snapshot $OldSnapshot -confirm:$false
This should give you a starting point at least.
Chris Nakagaki (Zsoldier)
Lucd, that worked perfectly. I have no problem creating a script for each VM (as there is not that many for now), but is there a way to create an array of vms in a single script. That way, schedule it once for multiple VMs? Thanks
That's perfectly possible.
How would you provide the array of VMs ? In a CSV file for example ?
The CSV would have the following layout
vmName vm1 vm2
Then you could do
$targetSnaps = 2 Import-Csv "C:\vm-names.csv" | %{ $vm = Get-VM -Name $_.vmName # Create daily snapshot $vm | New-Snapshot -Name ($_.vmName + " " + (Get-Date).ToShortDateString()) # Remove excess snapshots $snaps = $vm | Get-Snapshot | Sort-Object -Property Created -Descending if($snaps.Count -gt $targetSnaps){ $targetSnaps..($snaps.Count - 1) | %{ $snaps[$_] | Remove-Snapshot -Confirm:$false } } }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This looks great, a few more questions, where can I run it from and what extension will the script have? Thanks
You store the script in a .ps1 file and you can run it from the PowerCLI prompt.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Great, I noticed you are one of the most active people in the powershell community, I really appreciate that, again I'm as green as it gets when it comes to powershell do you know any other resources I can tap into to get up to speed?
Thank you very much.
It's perhaps not too recent but in my My PS library post I listed some of the resources I use.
There is another book coming that delves into PowerCLI, see my PowerCLI Book Update post.
Jonathan, one of the co-authors, has a very good introductory post. See his 10 Steps to Kick-Start Your VMware Automation with PowerCLI post.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference