VMware Cloud Community
tdubb123
Expert
Expert

how to deploy multiple vms from template?

how do I deploy more than 1 vm from a template?

I am using this command

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> New-VM -VMHost
-esx7.domain.com -name print0001 -Template 2003ENTR2_SP2_Tmpt -Datastore "LUN 24"
Reply
0 Kudos
11 Replies
RvdNieuwendijk
Leadership
Leadership

You could make a .csv file New-Vms.csv with all the information you need. Like this:

Name,VMHost,Template,Datastore
print0001,esx7.domain.com,2003ENTR2_SP2_Tmpt,"LUN 24"
print0002,esx8.domain.com,2003ENTR2_SP2_Tmpt,"LUN 25"


With this New-VMs.csv file you can create all the new virtual machines from a template with:

            

Import-Csv -Path New-VMs.csv | `
ForEach-Object {
  New-VM -VMHost $_.VMHost -Name $_.Name -Template $_.Template -Datastore $_.Datastore
}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
monahancj
Contributor
Contributor

This should get you started.

http://poshcode.org/2051

Takes a fixed format CSV and pumps out clones  from a template.  Easily modified to do clones of a VM or create new  VMs.  Some of the restrictions mentioned in the notes appear to be fixed  with VMware ESX v4.1.

Reply
0 Kudos
tdubb123
Expert
Expert

can i do this from the vMA?

Reply
0 Kudos
monahancj
Contributor
Contributor

Sorry, but I have no idea.

Reply
0 Kudos
tdubb123
Expert
Expert

this is my command

param ( $CsvFile = "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\New-VMs.csv" )
$worksheet = Import-Csv $CsvFile
$worksheet | ForEach-Object {
    new-VM -VMHost $_.VMHost `
-Name $_.Name `
-Template $_.Template `
        -MemoryMB $_.MemoryMB `
        -Datastore $_.Datastore `
        -NumCPU $_.NumCPU
}
but I am getting the error
PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> .\deployvms.ps
1
New-VM : Parameter set cannot be resolved using the specified named parameters.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\deployvms.ps1:
5 char:11
+     new-VM <<<<  -VMHost $_.VMHost `
    + CategoryInfo          : InvalidArgument: (:) [New-VM], ParameterBindingE
   xception
    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCor
   e.Cmdlets.Commands.NewVM
New-VM : Parameter set cannot be resolved using the specified named parameters.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\deployvms.ps1:
5 char:11
+     new-VM <<<<  -VMHost $_.VMHost `
    + CategoryInfo          : InvalidArgument: (:) [New-VM], ParameterBindingE
   xception
    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCor
   e.Cmdlets.Commands.NewVM
Reply
0 Kudos
LucD
Leadership
Leadership

Looks as if your .csv file doesn't have a VMHost column.


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

Reply
0 Kudos
tdubb123
Expert
Expert

I have vmhost in there. but when I use -Template It gives an error.

param ( $CsvFile = "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\New-VMs.csv" )
$worksheet = Import-Csv -Path $CsvFile
$worksheet | ForEach-Object {
         new-VM `
-VMHost $_.VMHost `
-Name $_.Name `
-Template $_.Template `
        -MemoryMB $_.MemoryMB `
        -Datastore $_.Datastore `
        -NumCPU $_.NumCPU
}
Reply
0 Kudos
LucD
Leadership
Leadership

Ok, I think I see what is wrong.

You are trying to use the parameterset Template but in that parameterset you can't use -MemoryMB and -NumCPU.

If you want to make changes to your template you have to use the -OSCustomizationSPec parameter.

If you look at the doc for the New-VM cmdlet you'll notice that under the Syntax header there are 4 different parametersets.

Due to the -Template parameter you fall in the Template parameterset and your other parameters have to come from the 3th entry (Template parameterset).


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

Reply
0 Kudos
monahancj
Contributor
Contributor

I handle this by first creating the VM with –template and –oscustomizationspec with a simple spec. Then separately I do set-vm for the cpu and ram, and I also set the VLAN, custom attributes, and a few other things. For all of them I pull the data from the CSV file.

-Chris

Reply
0 Kudos
tdubb123
Expert
Expert

Hi

but my template is already a syspreped template so I dont see any need for a OScustomizationspec paramenter.

But when I created one anyway and used this

param ( $CsvFile = "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\New-VMs.csv" )
$worksheet = Import-Csv -Path $CsvFile
$worksheet | ForEach-Object {
         new-VM `
-VMHost $_.VMHost `
-Name $_.Name `
-Template $_.Template `
-Datastore $_.Datastore `
-OSCustomizationSpec $_.OSCustomzationSpec
}
it fails
any ideas?
Reply
0 Kudos
LucD
Leadership
Leadership

Can't you create the new guest from the the template and after the creation use the Set-VM to change the CPU count and the memory ?

param ( $CsvFile = "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\New-VMs.csv" )
$worksheet = Import-Csv -Path $CsvFile
$worksheet | ForEach-Object {
        new-VM `
          -VMHost $_.VMHost `
          -Name $_.Name `
          -Template $_.Template `
          -Datastore $_.Datastore | `

        Set-VM -NumCpu $numCPU -MemoryMB $memoryMB

}


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

Reply
0 Kudos