3 Replies Latest reply on Jun 24, 2016 10:43 AM by Zsoldier

    Deploy multiple VMs to multiple hosts evenly?

    romatlo Enthusiast

      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

        • 1. Re: Deploy multiple VMs to multiple hosts evenly?
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Deploy multiple VMs to multiple hosts evenly?
            romatlo Enthusiast

            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}

            }

            • 3. Re: Deploy multiple VMs to multiple hosts evenly?
              Zsoldier Expert
              vExpert

              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.