VMware Cloud Community
aravinds3107
Virtuoso
Virtuoso
Jump to solution

Creating VM using Power CLI

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...

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

Reply
0 Kudos
20 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
aravinds3107
Virtuoso
Virtuoso
Jump to solution

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

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

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


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

Reply
0 Kudos
aravinds3107
Virtuoso
Virtuoso
Jump to solution

Server NameESX HostDatastoreTemplateCustomizationIP Address

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

aravinds3107
Virtuoso
Virtuoso
Jump to solution

Thanks Luc!!

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
Reply
0 Kudos
Fiction
Contributor
Contributor
Jump to solution

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 ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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
}


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

Reply
0 Kudos
Fiction
Contributor
Contributor
Jump to solution

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 ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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
}


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

Reply
0 Kudos
Fiction
Contributor
Contributor
Jump to solution

Thanks a lot, it's work just fine Smiley Happy

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

Reply
0 Kudos
max08
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos
max08
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

With a loop reading from the CSV file this could look something like this

Import-Csv "C:\guests.csv" -UseCulture | %{
    Get-OSCustomizationSpec $_.Customization | Get-OSCustomizationNicMapping | `
    Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $_."IP Address" `
        -SubnetMask 255.255.254.0 -DefaultGateway 192.136.55.1 -Dns 192.136.55.55
    $vm=New-VM -Name $_."Server Name" -Template $_.Template -Host $_."Esx Host" `
        -Datastore $_.Datastore -OSCustomizationSpec $_.Customization `
       
-Confirm:$false -RunAsync
....... }


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

Reply
0 Kudos
max08
Contributor
Contributor
Jump to solution

This is what I get when I attempt to deploy a single vm:

Set-OSCustomizationNicMapping : A positional parameter cannot be found that accepts argument '255.255.254.0'.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\powerCLI\working_scripts\csv\test_deploy.ps1:3
char:34
+     Set-OSCustomizationNicMapping <<<<  -IpMode UseStaticIP -IpAddress $_."IP Address" `-SubnetMask 255.255.254.0 -De
faultGateway 192.136.55.1 -Dns 192.136.55.55
    + CategoryInfo          : InvalidArgument: (:) [Set-OSCustomizationNicMapping], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetOSCustomizat
   ionNicMapping

This is my csv file:

Server NameEsx HostDatastoreTemplateCustomizationIP Address
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if something might have gone wrong with the copy/paste.

After the back-tick there should be <newline>.

I'll attache a file with that code


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

Reply
0 Kudos
max08
Contributor
Contributor
Jump to solution

Great it worked like a charm albeit except for a slight annoying error message:

The term '.......' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spe
lling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\powerCLI\working_scripts\csv\deployVM.ps1:8 ch
ar:8
+ ....... <<<<
    + CategoryInfo          : ObjectNotFound: (.......:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Those dots were taken from your original script, they are just there to indicate that you can eventually add other lines of code in there.

If there aren't any additional lines of code, just remove the line with the dots.


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

Reply
0 Kudos