VMware Cloud Community
nicholas1982
Hot Shot
Hot Shot

Run Task against All VM's in batches with delay

Hi All,

I would like to update the storage policy on 1000's VMs, the update actually should cause any resync so technical I could update all via the gui however i want to exercise caution and do them in batches with a delay of 5 mins between each batch, I'm just trying to figure out the code to do this.

Im first getting all VMs with old policy which might be 1000 for example, then run a foreach to do 50 at a time -runasync then wait 5 mins?

Nicholas
Reply
0 Kudos
4 Replies
jpsider
Expert
Expert

Sounds about right. This should get you close.

$count =0

foreach ($vm in $vms) {

$count++

<insert your task code>

if ($count -eq 50){

Start-sleep -s 300

$count = 0

}

}

Reply
0 Kudos
LucD
Leadership
Leadership

Are you considering the the Set-SpbmStoragePolicy cmdlet for this?

I'm afraid that doesn't have a RunAsync switch, meaning that a simple foreach loop will not do the trick.

One option is to start the cmdlet for each VM in a background job (with Start-Job), and then wait till all 50 jobs are finished, before continuing


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
nicholas1982
Hot Shot
Hot Shot

Hi Luc,

I'll be running this foreach loop, I wasn't aware -runasync wouldn't wouldn't work but now that I think about it i don't need to runasync just need the batches to have a delay of at least 5 mins allowing me to kill the script if any unusual behaviour occurs.

foreach ($VM in $VMs){

$VM | Set-SpbmEntityConfiguration -StoragePolicy $policy

Start-Sleep -Milliseconds 200

$VM | Get-HardDisk | Set-SpbmEntityConfiguration -StoragePolicy $policy

}

Nicholas
Reply
0 Kudos
LucD
Leadership
Leadership

You can pass multiple entities to that cmdlet, so you could do

$batchNr = 50

foreach($index in (0..([math]::Floor($vms.Count/$batchNr)))){

    $first = $index * $batchNr

    $last = [math]::Min((($index + 1) * $batchNr) - 1, $vms.Count - 1)

    Get-SpbmEntityConfiguration -VM $vms[$first..$last] |

    Set-SpbmEntityConfiguration -StoragePolicy $policy

    sleep (5 * 60)

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos