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
Nodes | Vmnames | Template name | Datastore | NumberOfCores | MemoryInMB | OS | Storage | ESXI |
2 | SERVM1,SERVM2 | RHEL_6_New_Template | SER_56_LUN_20 | 1 | 3584 | RHEL 6.8 | 20 | Host1 |
1 | SERVM6 | RHEL_6_New_Template | SER_56_LUN_21 | 2 | 7168 | RHEL 6.8 | 20 | Host2 |
3 | SEZVM1,SEZVM2,SEZVM3 | Win2k12_STD_Template | SEZ_45_LUN_4 | 4 | 14336 | Win 12 | 150 | Host3 |
4 | SEMVM46,SEMVM47,SEMVM48,SEMVM48 | Win2k12_ENT_Template | SEM_55_LUN_10 | 8 | 28672 | Win 12 | 1000 | Host4 |
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
Have a look on this Create VM using PowerCLI and csv file
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
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.
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
Thanks Sir Its working perfectly.
Is there any good docs for beginner's in powercli.
There are a few levels to start using PowerCLI and PowerShell.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD:)
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
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