VMware Cloud Community
fborges555
Enthusiast
Enthusiast

new-vm

Hi all.

I would like to deploy Multiples VMs from csv using a template

I want to use the new-VM and  accomplish is to read from a CSV file the following

Name( for the new VM), Template(to be use to create VM), CPU(for the new VM),mem(for the new VM),Disk(for the new vm)

can I use the 

$file=Import-Csv -Path .\VMs2Deploy.csv

$DS="myDS"

$TemplateName="my template'

foreach ($rown in $file){

New-VM -Template $TemplateName -Name $_.Name  -Datastore $DS  -NumCPU $_.CPU -MemoryGB $_.mem -DiskGB $_.Disk

}

0 Kudos
3 Replies
LucD
Leadership
Leadership

You could do something like this

$DS='myDS'
$TemplateName='my template'

Import-Csv -Path .\VMs2Deploy.csv -PipelineVariable row |
ForEach-Object -Process {
    New-VM -Template $TemplateName -Name $row.Name  -Datastore $DS  -NumCPU $row.CPU -MemoryGB $row.mem -DiskGB $row.Disk
}


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

0 Kudos
fborges555
Enthusiast
Enthusiast

L.

I was running your script and I get the following

"New-VM : Parameter set cannot be resolved using the specified named parameters.
At line:3 char:5
+ New-VM -Template $TemplateName -Name $row.Name -Datastore $DS - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-VM], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM."

 

I have checked misspellings on the csv file and code as well and all is correct as per your suggestion , can you help?

 

Thanks a bunch

 

0 Kudos
LucD
Leadership
Leadership

The Template parameterset of the New-VM cmdlet doesn't allow NumCpu, MemoryGB, and DiskGB parameters.

You could use the New-VM cmdlet without those parameters.
And then use the Set-VM cmdlet to alter the CPU count and Memory allocated after the New-VM completes.
And use the Set-Harddisk cmdlet to change the size of the harddisk.

$DS='myDS'
$TemplateName='my template'

Import-Csv -Path .\VMs2Deploy.csv -PipelineVariable row |
ForEach-Object -Process {
    $vm = New-VM -Template $TemplateName -Name $row.Name  -Datastore $DS
    Set-VM -VM $vm -NumCPU $row.CPU -MemoryGB $row.mem -DiskGB
    Get-Harddisk -VM $vm | Set-Harddisk -CapacityGB $row.Disk
}

 


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

0 Kudos