VMware snapshots from array

Hi,

I am starting with powershell to create snapshots before running windows update.

i had created this loop, it works but this will only create 1 snapshot simultaneously, i want to 2 servers at the same time to create snapshots and not more then 2.

$VMs =              Get-Content arr_vCenter_VMs.txt
foreach($tmpVM_Name in $VMs)
{
    $dtMDYT = Get-Date -Format G
    Get-VM $tmpVM_Name | New-Snapshot -Name “$tmpVM_Name $dtMD” -Description “$dtMDYT – Snapshot prior to Windows Updates Installation via UpdateAutoPilot-vCenter.ps1” -Memory:$true -Confirm:$false | Out-File C:\VMWare_snapshots.log -append -Width 500
}

If you use foreach, then its sequential and it will process one item at a time. There is no parallelism in foreach in Windows PowerShell.

Btwn, Why you want this to be in parallel and if you need it in parallel, why to restrict at 2 VMs in parallel ?

Hi,

First all thank you for your reply.

We’ve discovered that creating snapshots for 2 servers is the optimal setting not to have a high cpu load on our servers. if we do 1 it will take to much time.

foreach is the most simple loop i can use, but maybe i should go to a while loop with a counter, but not having many experience with that.

Maybe something like this. I have no way to test, but the concept might work.

$VMs = Get-Content arr_vCenter_VMs.txt
for(i$=0; i$ -lt $VMs.Count; $i++)
{
$dtMDYT = Get-Date -Format G
Start-Job -Name 'cloneVM1' -ScriptBlock {Get-VM $VMs[$i] | New-Snapshot -Name “$VMs[$i] $dtMD” -Description “$dtMDYT – Snapshot prior to Windows Updates Installation via UpdateAutoPilot-vCenter.ps1” -Memory:$true -Confirm:$false | Out-File C:\VMWare_snapshots.log -append -Width 500}

$i++

Start-Job -Name 'cloneVM2' -ScriptBlock {Get-VM $VMs[$i] | New-Snapshot -Name “$VMs[$i] $dtMD” -Description “$dtMDYT – Snapshot prior to Windows Updates Installation via UpdateAutoPilot-vCenter.ps1” -Memory:$true -Confirm:$false | Out-File C:\VMWare_snapshots.log -append -Width 500}

Get-Job | Wait-Job

}