- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Powershell beginner, here. ![]()
Thanks,
romatlo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Blog: https://tech.zsoldier.com
Twitter: @zsoldier