VMware Cloud Community
romatlo
Enthusiast
Enthusiast
Jump to solution

Deploy multiple VMs to multiple hosts evenly?

Hello folks!

I've written a short script to deploy multiple VMs to multiple hosts randomly.

But I would rather deploy a VM to each host in an array and then start from the beginning again until number of VMs to deploy runs out.  Spreading the deployment load as evenly as possible.

Would anyone have a suggestion?  Nested loop example?

pastedImage_1.png

Powershell beginner, here.  Smiley Happy

Thanks,

romatlo

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

One way of doing this is with the modulo operator (%), something like this

$numVMs =11

$tgtEsx = Get-Cluster 'Westcreek' | Get-VMHost -Name z420*

1..$numVMs | %{

    New-VM -Name "Test$($_)" -VMHost $tgtEsx[$_%$tgtEsx.Count]

}


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

One way of doing this is with the modulo operator (%), something like this

$numVMs =11

$tgtEsx = Get-Cluster 'Westcreek' | Get-VMHost -Name z420*

1..$numVMs | %{

    New-VM -Name "Test$($_)" -VMHost $tgtEsx[$_%$tgtEsx.Count]

}


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

0 Kudos
romatlo
Enthusiast
Enthusiast
Jump to solution

Thank you for the feedback!

I ended up finding a solution based on your feedback and others.

Here is the script I ended up with that is working well so far for what I need it for immediately.

Thanks again!

$VMHOSTS = Get-Cluster 'Westcreek' | Get-VMHOST | Sort name

$NumVMs = 3

$NEW_VMS = (1..$NumVMs)

$myTemplate = Get-Template -Name Win7temp

$mySpecification = Get-OSCustomizationSpec -Name win7

$i = 0

ForEach ($VM in $NEW_VMS) {

New-VM -Name test$($VM) -VMHost $($VMHOSTS[$i]) -ResourcePool Hosts -Template $myTemplate -OSCustomizationSpec $mySpecification -Datastore VMs2 -DiskStorageFormat Thin

$i++

If ($i -ge $VMHOSTS.count) {$i=0}

}

Zsoldier
Expert
Expert
Jump to solution

Great job figuring out your solution.  On the flip-side.  Assuming DRS is running and set to automatic, DRS will power-on the newly deployed VM's in a distributed fashion.  Meaning you could deploy all VM's to one host in the cluster and be fine since DRS would distribute them evenly for you.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier