6 Replies Latest reply on Aug 17, 2010 10:07 AM by monahancj

    PowerCLI script to deploy vm's from a template

    JimmyDean Enthusiast

      Hey all,

       

       

      I have this script part way done. My questions are:

       

       

       

      1.  I want to find out the nubmber of "Winxp-X" are currently deployed then I want to add more. I don't want to use a CSV or anyother format to inmport from. Yes i'm that lazy. For some reason i can't figure it out!

       

       

       

      2. On the line that askes "What esx host do you want to use" i would like to just hit "1" for esx1  vs typing out the name of the host.

       

       

       

      Can the above questions be done? if so HELP! I have been working on this for days. Thanks!

       

       

       

       

       

      1. Yes i a Connect-VIServer -Server blah blah blah... Just removed.

       

      {

      {

      $freespaceCalc = @{ Name = "FreespaceGB"; Expression = { ::Round($_.FreeSpaceMB * 1MB / 1GB ) } }

      $capacityCalc = @{ Name = "CapacityGB"; Expression = { ::Round($_.CapacityMB * 1MB / 1GB) } }

       

       

       

      1. User enters how manny agents are requred

      $agentTotal = Read-Host "Enter how many WinXp Agents you need."

      $agnetTotal = $agentTotal

      Get-Datastore | Select Name, $freespaceCalc, $capacityCalc |Sort-Object Name | ft -Force -AutoSize| Out-Default

      $datastore = Read-Host "What DataStore do you want to use?"

      Get-VMHost | Select-Object Name | Sort Name |ft -Force -AutoSize | Out-Default

      $esxHost = Read-Host "What Esx-Host Do you want to use?"

       

      $XP = Get-VM -Name WinXP-*

      $totalXPCount = @($XP).Length

      $totalXP = (1 + $totalXPCount)

      for($adt = ($totalXP + $agentTotal);$totalXP -lt $adt; $totalXP++){

       

      if($totalXPCount -eq $adt)

      {

      break

      }

       

      $machineName = "WinXP-$totalXPCount"

      $template = "XP-32 Template"

      $osspec = Get-OSCustomizationSpec XP

       

      foreach ($vm in $machineName)

      {$vm = New-VM -Name $vm -Template $template -Host $esxHost -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }

      }

        • 1. Re: PowerCLI script to deploy vm's from a template
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          1) The following script snippet will run through the loop the number of missing guests times.

          $agentTotal = [int](Read-Host "Enter how many WinXp Agents you need.")
          
          $totalXPCount = (Get-VM -Name WinXP-* | Measure-Object).Count
          if($totalXPCount -lt $agentTotal){
               # Add missing XP guests
               for($adt = $totalXPCount + 1; $adt -le $agentTotal; $adt++){
                    Write-Host "Adding guest" $adt
                    # Execute the New-Vm part
               }
          }
          

           

          2) The following script snippet displays the hostnames prefixed with a sequential number.

          With the number, in variable $hostIndex,  you can access the selected host in the array $hostList

          $hostList = Get-VmHost | select Name | Sort-Object
          $i = 1
          $hostList | %{Write-Host $i ":" $_.Name; $i++}
          $hostIndex = Read-Host "Enter a number (1 -" $hostList.Count ")"
          Write-Host "You selected host" $hostIndex "with hostname" $hostList[$hostIndex - 1].Name
          

           

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          1 person found this helpful
          • 2. Re: PowerCLI script to deploy vm's from a template
            wila Guru
            vExpertCommunity WarriorsUser Moderators

            Hi,

             

            I did notice that you said "I don't want to use CSV" but did you see this document? Deploy VM's based on a template from a .csv



            --

            Wil

            _____________________________________________________

            VI-Toolkit & scripts wiki at http://www.vi-toolkit.com

            | Author of Vimalin. The virtual machine Backup app for VMware Desktop Products
            | Vimalin : Automated backups for VMware Fusion and VMware Workstation Professional
            | More info at https://www.vimalin.com
            | Twitter @wilva
            | VMware Wiki at http://www.vi-toolkit.com
            • 3. Re: PowerCLI script to deploy vm's from a template
              JimmyDean Enthusiast

              LucD,

               

              Your post is just want i needed. But now i get the following:

               

               

               

              New-VM : 8/12/2009 4:32:00 PM    New-VM        Could not find VMHost with name '1'.

              At C:\Documents and Settings\Administrator\Desktop\Scripts\Agent-Deploy.ps1:92 char:19

              +                     {$vm = New-VM  <<<< -Name $vm -Template $template -Host $hostIndex -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }

              New-VM : Value cannot be found for the mandatory parameter VMHost

              At C:\Documents and Settings\Administrator\Desktop\Scripts\Agent-Deploy.ps1:92 char:19

              +                     {$vm = New-VM  <<<< -Name $vm -Template $template -Host $hostIndex -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }

               

               

               

               

               

               

               

               

               

              What did I do wrong?

               

               

               

               

               

               

               

               

               

              Thankx!

               

               

               

               

               

               

               

               

              Current            "** Create Win XP **";

                      $freespaceCalc = @{ Name = "FreespaceGB"; Expression = { ::Round($_.FreeSpaceMB * 1MB / 1GB ) } }

                      $capacityCalc = @{ Name = "CapacityGB"; Expression = { ::Round($_.CapacityMB * 1MB / 1GB) } }

               

               

                           

              1. User enters how manny agents are requred

                          $agentTotal = (Read-Host "Enter how many WinXp Agents you need.")

               

                          $totalXPCount = (Get-VM -Name WinXP-* | Measure-Object).Count

                          if($totalXPCount -lt $agentTotal){

                               

              1. Add missing XP guests

                              for($adt = $totalXPCount + 1; $adt -le $agentTotal; $adt++){

                                   

              1. Execute the New-Vm part

                              }

                          }

               

               

                          Get-Datastore | Select Name, $freespaceCalc, $capacityCalc |Sort-Object Name | ft -Force -AutoSize| Out-Default

                          $datastore = Read-Host "What DataStore do you want to use?"

               

                          $hostList = Get-VmHost | select Name | Sort-Object

                          $i = 1

                          $hostList | %{Write-Host $i ":" $_.Name; $i++}

                          $hostIndex = Read-Host "Enter a number (1 -" $hostList.Count ")"

                          Write-Host "You selected host" $hostIndex "with hostname" $hostList[$hostIndex - 1].Name

               

                          $XP = Get-VM -Name WinXP-*

                          $totalXPCount = @($XP).Length

                          $totalXP = ($totalXPCount + 1)

               

                          for($adt = ($totalXP + $agentTotal);$totalXP -lt $adt; $totalXP++){

               

                              if($totalXPCount -eq $adt)

                                  {

                                      break

                                  }

               

                              $machineName = "WinXP-$totalXPCount"

                              $template = "XP-32 Template"

                              $osspec = Get-OSCustomizationSpec XP

               

                              foreach ($vm in $machineName)

                                  {$vm = New-VM -Name $machineName -Template $template -Host $hostIndex -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }

                          }

                          Disconnect-VIServer -Server $DefaultVIServer -Confirm:$false

              • 4. Re: PowerCLI script to deploy vm's from a template
                LucD Guru
                vExpertCommunity WarriorsUser Moderators

                The variable $hostIndex contains the index in array $hostList of the selected host.

                This is the corrected script.

                Note that the forum SW has problems with square brackets so you better use the attached file.

                # Current "** Create Win XP **";
                $freespaceCalc = @{ Name = "FreespaceGB"; Expression = { [Math]::Round($_.FreeSpaceMB * 1MB / 1GB ) } }
                $capacityCalc = @{ Name = "CapacityGB"; Expression = { [Math]::Round($_.CapacityMB * 1MB / 1GB) } }
                
                # User enters how manny agents are requred
                $agentTotal = [int](Read-Host "Enter how many WinXp Agents you need.")
                
                $totalXPCount = (Get-VM -Name WinXP-* | Measure-Object).Count
                if($totalXPCount -lt $agentTotal){
                     for($adt = $totalXPCount + 1; $adt -le $agentTotal; $adt++){
                
                          Get-Datastore | Select Name, $freespaceCalc, $capacityCalc |Sort-Object Name | ft -Force -AutoSize| Out-Default
                          $datastore = Read-Host "What DataStore do you want to use?"
                
                          $hostList = Get-VmHost | select Name | Sort-Object
                          $i = 1
                          $hostList | % {Write-Host $i ":" $_.Name; $i++}
                          $hostIndex = Read-Host "Enter a number (1 -" $hostList.Count ")"
                          Write-Host "You selected host" $hostIndex "with hostname" $hostList[$hostIndex - 1].Name
                
                          $machineName = "WinXP-$adt"
                          $template = "XP-32 Template"
                          $osspec = Get-OSCustomizationSpec XP
                
                          foreach ($vm in $machineName){
                               $vm = New-VM -Name $machineName -Template $template -Host $hostList[$hostIndex - 1].Name -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }
                     }
                }
                

                 

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: PowerCLI script to deploy vm's from a template
                  JimmyDean Enthusiast

                   

                  LucD,

                   

                   

                   

                   

                   

                  Thank you again! This worked great! I have been jammed at work. Sorry for the delay. Thank you again very much!

                   

                   

                   

                   

                   

                  J

                   

                   

                  • 6. Re: PowerCLI script to deploy vm's from a template
                    monahancj Novice

                     

                    This might help you figure out how to use a CSV file for input.

                     

                     

                    http://poshcode.org/2051