Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 11, 2022 01:18 am GMT

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

    $snapshot_name = "Example Snapshot Name"
$snapshot_description = "Example Snapshot Description"
$num_concurrent_jobs_limit = 8
foreach ($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    }}




Code Explanation

  • 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 the foreach 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.

Resources


Original Link: https://dev.to/tomkanabay/automate-snapshots-of-multiple-vms-concurrently-using-powercli-4kk5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To