Dears, In my POC environment requirement is to create snapshots on several times on multiple VMs in a months and also removal, so wondering if someone can help me with the script to automate the task - snapshot creation/deletion on multiple servers with preferred Name and description, and should omit Virtual Memory. thanks.
2) Try like this
$vmNames = 'vm1','vm2','vm3'
$description = Read-Host -Prompt "Enter text that shall appear in the snapshot Description"
# Remove snapshots
Get-VM -Name $vmNames | Get-Snapshot | where{$_.Description -match $description} |
Remove-Snapshot -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
How were you planning to schedule the script?
With the Windows Task Scheduler?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No plan is like manually run the scirpt, because the timeframe may not same for all the times.
Ok, but you to give some details on what exactly you want to.
Is it a snapshot for all VMs in that environment?
If not, how do you select the VMs?
What name shall the snapshots have?
For the removal, does the script remove all the existing snapshots by default, or do you use specific criteria to remove specific snapshots?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Well,not all the VMs in the environment, i have certain list (Count = 25) which I only wants to take snapshot.
For example I want to give the name as "Test on (todays date)".
I want to delete the snapsthots on all of them after 3 days which were taken on day 1, again I'm not going to touch anyother snapshots in the inventory which may taken for any other tests. Kindly let me know if any additional information required.
Try something like this
$vmNames = 'vm1','vm2','vm3'
$now = Get-Date
# Remove old snapshots (> 3 days)
Get-VM -Name $vmNames | Get-Snapshot | where{($now - $_.Created).TotalDays -gt 3} |
Remove-Snapshot -Confirm:$false
# Create new snapshots
Get-VM -Name $vmNames | New-Snapshot -Name "Test on $(Get-Date -Format 'dd/MM/yyyy hh:mm')" -Description "Test" -Memory:$false -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much for your immediate response, well two points.
1. while creation of snapshot it is giving result on powercli screen where the powerstate of VM is showing as powered off which is flase, VMs are powered ON, can you suggest on it.
2. Removal of snapshot script is looking for only "greater than 3 days", if scirpt look for removal based upon snapshot title or description would be really help, otherwise if there are anyother snapshots which are taken morethan 3 days which I dont have to delete may also delete.
Thanks again.
1. That is because the Memory is not captured in the snapshot
2. And how would the script determine, based on the name or description, if the snapshot can be removed or not?
You have to give me the logic, if you want it scripted
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
1. Got it now, Thank you so much.
2. Can you please help me to determine the script to remove the snapshot based on the "Description" please.
Thanks again.
2) Try like this
$vmNames = 'vm1','vm2','vm3'
$description = Read-Host -Prompt "Enter text that shall appear in the snapshot Description"
# Remove snapshots
Get-VM -Name $vmNames | Get-Snapshot | where{$_.Description -match $description} |
Remove-Snapshot -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, I'm back to seek your suggestion, if I would like to delete or create a snapshot morethan 20 which are not same for everytime it is becoming time taken to place all of them after $vmNames with single codes, so can you please propose if there is way around to paste all of servers in notepad and that will be called while create/delete the snapshot.
Many Thanks.
Sorry, not sure I get the question.
Could you perhaps give/show an example of what you are trying to do?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference