1 2 Previous Next 20 Replies Latest reply on Sep 9, 2011 12:49 PM by max08

    Creating VM using Power CLI

    aravinds3107 Master

      Hi,

       

      I am tying to deploy VM's using power CLI but i get the below error as below:

       

      I am using the below script to deploy VM from exisitin template

       

      New-vm -Name testvm005 -VMhost host -Template TMPL_W2K3_STD_SP2_R2_x86 -Datastore datstorename  -OSCustomizationspec W2K3-ENT-R2-SP2-32bit

       

      New-VM : 5/25/2011 10:37:09 AM    New-VM        The specified parameter 'Templa
      te' expects a single value, but your name criteria 'TMPL_W2K3_STD_SP2_R2_x86' c
      orresponds to multiple values.

       

      Can somone help me out on this...

        • 1. Re: Creating VM using Power CLI
          LucD Guru

          It looks as if you have more than 1 template with that name in your vCenter.

          Do a

          Get-Template

          to check.

          If you want to use a specific template, you will have to find a distinguishing feature.

          Suppose for example that the template you want to use is located in a folder called MyTemplates, then you can do

          $template = Get-Template -Name TMPL_W2K3_STD_SP2_R2_x86 -Location (Get-Folder -Name MyTemplates)

          New-vm -Name testvm005 -VMhost host -Template $template -Datastore datstorename  -OSCustomizationspec W2K3-ENT-R2-SP2-32bit

          • 2. Re: Creating VM using Power CLI
            aravinds3107 Master

            Thanks Luc,

             

            That Worked !!! i am deploying multiple VM's using the same template and use CSV to fetch to input all details.Could you please help me on this also

            • 3. Re: Creating VM using Power CLI
              LucD Guru

              Can you show what information you have in the CSV file ?

              The column header row will tell us which properties to use.

              • 4. Re: Creating VM using Power CLI
                aravinds3107 Master

                Server NameESX HostDatastoreTemplateCustomizationIP Address

                • 5. Re: Creating VM using Power CLI
                  LucD Guru

                  You can do this like this

                   

                   

                  Import-Csv "C:\guests.csv" -UseCulture | %{
                    New-vm -Name $_."Server Name" -VMhost $_."ESX Host" -Template $_.Template -Datastore $_.Datastore  -OSCustomizationspec $_.Customization
                  }
                  

                   

                  Note that you have to put the propertyname between quotes if it contains a blank.

                  1 person found this helpful
                  • 6. Re: Creating VM using Power CLI
                    aravinds3107 Master

                    Thanks Luc!!

                    • 7. Re: Creating VM using Power CLI
                      Fiction Novice

                      Hello

                       

                      I try your script, it is very good.

                      Is it possible to add the number of cpu with the value NumCpu in the csv file ?

                      • 8. Re: Creating VM using Power CLI
                        LucD Guru

                        Sure, provided you add a column called NumCPU to the CSV file, you can do

                        Import-Csv "C:\guests.csv" -UseCulture | %{
                          New-vm -Name $_."Server Name" -VMhost $_."ESX Host" -Template $_.Template -Datastore $_.Datastore -NumCpu $_.NumCpu -OSCustomizationspec $_.Customization
                        }
                        • 9. Re: Creating VM using Power CLI
                          Fiction Novice

                          Thank You for your answer, but i have an error

                           

                          [vSphere PowerCLI] C:\> .\esx.ps1
                          New-VM : Le jeu de paramètres ne peut pas être résolu à l'aide des paramètres n
                          ommés spécifiés.
                          Au niveau de C:\esx.ps1 : 2 Caractère : 9
                          +   New-vm <<<<  -Name $_."Server Name" -VMhost $_."ESX Host" -Template $_.Temp
                          late -Datastore $_.Datastore -NumCpu $_.NumCpu -OSCustomizationspec $_.Customiz
                          ation
                              + CategoryInfo          : InvalidArgument: (:) [New-VM], ParameterBindingE
                             xception
                              + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCor
                             e.Cmdlets.Commands.NewVM

                           

                          Any idea ?

                          • 10. Re: Creating VM using Power CLI
                            LucD Guru

                            Yes, when creating a new VM from a template you are using the Template parameterset.

                            Unfortunately, the NumCpu parameter is not in the Template parameterset.

                             

                            As a bypass, you can use the Set-VM cmdlet, after New-VM, to change the number of CPUs.

                             

                            Import-Csv "C:\guests.csv" -UseCulture | %{
                              New-vm -Name $_."Server Name" -VMhost $_."ESX Host" -Template $_.Template -Datastore $_.Datastore -OSCustomizationspec $_.Customization
                              Set-VM -Name $_."Server Name" -NumCpu $_.NumCpu
                            }
                            • 11. Re: Creating VM using Power CLI
                              Fiction Novice

                              Thanks a lot, it's work just fine

                              I just change set-vm -name by set-vm -vm

                              • 12. Re: Creating VM using Power CLI
                                max08 Enthusiast

                                Hi LucD

                                 

                                Question regarding your script, if I wanted to set a static IP using that script how would I do that?  I ran your script with a .csv file using the same headers as above, but the virtual machine is simply getting DHCP rather than using the specified IP address in the .csv.

                                 

                                Server NameEsx HostDatastoreTemplateCustomizationIP Address
                                csv_testhost06host06_storageTemplate_testtest10.102.31.249

                                 

                                The vm is created but the static IP is not assigned, it simply uses DHCP.  I have this working via array with static IP and not using a .csv file but I would prefer csv, can you help me out?

                                • 13. Re: Creating VM using Power CLI
                                  LucD Guru

                                  The DHCP settings is the default in an OSCustomization definition.

                                  You could use the Set-OSCustomizationNicMapping cmdlet to change the NIC settings of the OSCustomization definition before using the New-VM cmdlet.

                                  Something like this

                                  Get-OSCustomization -Name MyCustSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpAddress $_.IpAddress

                                  Note that you will need to specify additional parameters besides the IP address!

                                   

                                  If you don't want to change your OSCustomization definition, you can create a temporary clone before you start creating the new VMs.

                                  Like this

                                  $tempOSSpec = New-OSCustomizationSpec -OSCustomizationSpec MyCustSpec

                                  You can now use the definition available in $tempOSSpec for the rest of the script and your original deifnition will remain unchanged.

                                  • 14. Re: Creating VM using Power CLI
                                    max08 Enthusiast

                                    Thanks but I have thing working already via arrays but its very tedious.  How do I append the IP/Gateway/DNS when using a .csv file?

                                     

                                    The customization spec is configured with a static IP and what I am using now looks like this:

                                    ...........

                                    $array = "hostname"

                                    $iparray = "192.136.55.4"

                                    $a= 0

                                    ....

                                    foreach ($vm in $array)
                                    {
                                    Get-OSCustomizationSpec $customization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IParray[$a] -SubnetMask 255.255.254.0 -DefaultGateway 192.136.55.1 -Dns 192.136.55.55
                                    $vm=New-VM -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -OSCustomizationSpec $customization -Confirm:$false -RunAsync

                                    .......

                                     

                                     

                                    Any help would be appreciated.

                                    1 2 Previous Next