VMware Cloud Community
powercliauto
Contributor
Contributor
Jump to solution

vms deploy from template

Hello

I had requirement to deploy vms from a template where vms details are pulled from template as the node count is given in csv based on the node count in a row that many vms should create in series.

as i am new I can able to deploy single vm at once but i am faling to deploy multiple vms at once.

Can someone please write a script for my requirement

the csv file contains like this

For example if the node count is 2 it show create vms with names SERVM1,SERVM2 (the number count will increase if i increase the node count  in the csv like SERVM1,SERVM2,SERVM3,SERVM4)

Same for all the vms

       

NodesVmnamesTemplate nameDatastoreNumberOfCoresMemoryInMBOSStorageESXI
2SERVM1,SERVM2RHEL_6_New_TemplateSER_56_LUN_2013584RHEL 6.820Host1
1SERVM6RHEL_6_New_TemplateSER_56_LUN_2127168RHEL 6.820Host2
3SEZVM1,SEZVM2,SEZVM3Win2k12_STD_TemplateSEZ_45_LUN_4414336Win 12150Host3
4SEMVM46,SEMVM47,SEMVM48,SEMVM48Win2k12_ENT_TemplateSEM_55_LUN_10828672Win 121000

Host4

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There is a few things you need to take into account.

When you clone a new VM from a Template, you can't set all parameters on the New-VM cmdlet.

The new VM takes for example the NumCpu, memory and disksize from the Template.

If you want to change those, you will have to use a Set-VM after the VM has been created.

To run the creation of new VMs in the background, you use the RunAsync switch on the New-VM cmdlet.

You will have to wait for the completion of these background tasks with the help of the Get-Task cmdlet, or by looking at the events generated by the creation of the VM.

To have multiple names in one column in a CSV file, you will have to separate the values with something else than a comma.

I used the '|" in the example code.

A quick and dirty example

# CSV layout

# "Nodes","VmNames","TemplateName","Datastore","EsxName"

# "2","ServM1|ServM2","RHEL_6_New_Template","SER_56_LUN_20","Host1"

# "1","ServM6","RHEL_6_New_Template","SER_56_LUN_21","Host2"

#

$fileName = 'C:\Scripts\deploy.csv'

foreach($row in (Import-Csv -Path $fileName -UseCulture)){

    $row.VmName.Split('|') | %

        New-VM -Name $_ -Template $row.TemplateName -Datastore $row.Datastore -VMHost $row.EsxName -RunAsync

    }

}


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

View solution in original post

0 Kudos
9 Replies
vijayrana968
Virtuoso
Virtuoso
Jump to solution

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is a few things you need to take into account.

When you clone a new VM from a Template, you can't set all parameters on the New-VM cmdlet.

The new VM takes for example the NumCpu, memory and disksize from the Template.

If you want to change those, you will have to use a Set-VM after the VM has been created.

To run the creation of new VMs in the background, you use the RunAsync switch on the New-VM cmdlet.

You will have to wait for the completion of these background tasks with the help of the Get-Task cmdlet, or by looking at the events generated by the creation of the VM.

To have multiple names in one column in a CSV file, you will have to separate the values with something else than a comma.

I used the '|" in the example code.

A quick and dirty example

# CSV layout

# "Nodes","VmNames","TemplateName","Datastore","EsxName"

# "2","ServM1|ServM2","RHEL_6_New_Template","SER_56_LUN_20","Host1"

# "1","ServM6","RHEL_6_New_Template","SER_56_LUN_21","Host2"

#

$fileName = 'C:\Scripts\deploy.csv'

foreach($row in (Import-Csv -Path $fileName -UseCulture)){

    $row.VmName.Split('|') | %

        New-VM -Name $_ -Template $row.TemplateName -Datastore $row.Datastore -VMHost $row.EsxName -RunAsync

    }

}


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

0 Kudos
powercliauto
Contributor
Contributor
Jump to solution

Thanks LucD for kind help. Just vm creation is enough I will set other parameters later.

To have multiple names in one column in a CSV file, you will have to separate the values with something else than a comma.

Ok undestood. If I give a vm name testvm can it increase the number by looking at the node count.

something like this.

In the csv if the node count is 3 and if I keep vm name as testvm by looking the vmname it should create testvm1 , testvm2,testvm3 will this logic possible it should just create vms in series by looking the nodecoun in csv.

if that logic works I'm can proceed to create in that way.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This should work like that

# CSV layout

# "Nodes","VmName","TemplateName","Datastore","EsxName"

# "2","ServA","RHEL_6_New_Template","SER_56_LUN_20","Host1"

# "1","ServB","RHEL_6_New_Template","SER_56_LUN_21","Host2"

#

$fileName = 'C:\Scripts\deploy.csv'

foreach($row in (Import-Csv -Path $fileName -UseCulture)){

    for($i=1; $i -le $row.Nodes; $i++){

        New-VM -Name "$($row.VmName)$($i)" -Template $row.TemplateName -Datastore $row.Datastore -VMHost $row.EsxName -RunAsync

    }

}


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

0 Kudos
powercliauto
Contributor
Contributor
Jump to solution

Thanks Sir Its working perfectly.

Is there any good docs for beginner's in powercli.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are a few levels to start using PowerCLI and PowerShell.


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

powercliauto
Contributor
Contributor
Jump to solution

Thanks LucD:)

0 Kudos
zgs
Contributor
Contributor
Jump to solution

On this basis, I would like to ask how you can add disk to the virtual machine from the template by powershell.Could you please give me a reply?Thank you very much

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You will need to use the New-Harddisk cmdlet after the New-VM cmdlet.

You can't do this on the New-VM when you create a VM from a template.
Or you can also change the template, when the extra harddisk is the new standard.


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

0 Kudos