An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
April 9, 2022 03:02 am GMT
Original Link: https://dev.to/thomaskcode/automate-snapshots-of-multiple-vms-concurrently-using-powercli-4fh8
Automate Snapshots of Multiple VMs Concurrently Using PowerCLI
This is a PowerShell / PowerCLI script I wrote to automate snapshots of multiple VMs concurrently using PowerCLI. To prevent overwhelming vCenter, a limit specifies the maximum number of concurrent tasks.
Code Description
- For each
$vm
in$vms
, the script starts an asynchronous snapshot task that allows the script to continue without waiting for the task to complete. - A
while
loop implements a check/sleep cycle that fetches the number of running snapshot tasks, and sleeps if the number of tasks is greater than the limit. - When the number of snapshot tasks is less than the limit, the
while
loop exits, and theforeach
loop progresses to the next VM. - The script outputs an error message if it cannot start a snapshot for a VM, and exits if it's unable to fetch the current number of snapshot tasks.
Code
$snapshot_name = "Example Snapshot Name"
$snapshot_description = "Example Snapshot Description"
$num_concurrent_jobs_limit = 8foreach ($vm in $vms) { try { New-Snapshot -VM $vm -Name $snapshot_name -Description $snapshot_description -Memory:$false -Quiesce:$false -RunAsync:$true -Confirm:$false -Server $vcenter_server } catch { Write-Host("Error creating snapshot for $vm on $vcenter_server.") $ErrorMessage = $_.Exception.Message Write-Host($ErrorMessage) } try { $current_snapshot_tasks = Get-Task | where {$_.Name -eq 'CreateSnapshot_Task' -and 'Running','Queued' -contains $_.State} while ($current_snapshot_tasks.count -gt $num_concurrent_jobs_limit) { sleep 5 $current_snapshot_tasks = Get-Task | where {$_.Name -eq 'CreateSnapshot_Task' -and 'Running','Queued' -contains $_.State} } } catch { Write-Host("Error getting snapshot tasks on $vcenter_server.") $ErrorMessage = $_.Exception.Message Write-Host($ErrorMessage) exit }}
Resources
VMware PowerCLI
Original Link: https://dev.to/thomaskcode/automate-snapshots-of-multiple-vms-concurrently-using-powercli-4fh8
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To