VMware Cloud Community
leitsu
Contributor
Contributor

Create a new VM

Hi

Does anyone have a script to create VMs? The datails of the VMs are specified in a csv file. The script looks into the csv file to build the VM(without OS installed).

temp.PNG

0 Kudos
6 Replies
LucD
Leadership
Leadership

You will have to provide an ESXi server on the New-VM as well.

Then you could do something like this

Import-Csv C:\list.csv -UseCulture | %{
  New-VM -Name $_.Name -VMHost $esx -Datastore $_.Datastore -DiskGB $_.DiskSize -GuestId "windows7Server64Guest"
}

Note that the GuestId requires a value from the VirtualMachineGuestOSIdentiefier enumeration.


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

0 Kudos
leitsu
Contributor
Contributor

Thanks!

What if I have multiple disks for the VM?

t2.PNG

0 Kudos
LucD
Leadership
Leadership

It would be handier if the disksizes are on the same row in the CSV.

Name,Datastore,DiskSize1,DiskSize2,DiskSize3

VM1,ds1,10,20,25

VM2,ds1,10,20,0

Then you could do

Import-Csv C:\list.csv -UseCulture | %{
  New-VM -Name $_.Name -Datastore $_.Datastore -DiskGB $_.DiskSize1,$_.DiskSize2,$_.DiskSize3 -GuestId "windows7Server64Guest"
}

If the DiskSize is 0, there will be no harddisk created


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

0 Kudos
SachinRanade
Contributor
Contributor

Extract the files to a folder and execute after updating the NewVMs.csv list with your VM requirements

0 Kudos
leitsu
Contributor
Contributor

Hi Luc

What if I use wildcard for my datastore? will the code you provide work?

Name,Datastore,DiskSize1,DiskSize2,DiskSize3

VM1,ds1*,10,20,25

VM2,ds1*,10,20,0

Import-Csv C:\list.csv -UseCulture | %{
  New-VM -Name $_.Name -Datastore $_.Datastore -DiskGB $_.DiskSize1,$_.DiskSize2,$_.DiskSize3 -GuestId "windows7Server64Guest"
}

0 Kudos
LucD
Leadership
Leadership

No, the Datastore parameter expects a single value.

But you can use masking with a small change. Something like thisfor example

Import-Csv C:\list.csv -UseCulture | %{
  New-VM -Name $_.Name -Datastore (Get-Datastore $_.Datastore | Get-Random) -DiskGB $_.DiskSize1,$_.DiskSize2,$_.DiskSize3 -GuestId "windows7Server64Guest"
}

The new VM will be placed on a randomly chosen datastore from the masked specs you give in the CSV.

But if you want to do this why don't you set up a datastorecluster (SDRS) ?

You can pass the datastorecluster name on the Datastore parameter and then SDRS will chose a datastore from the datastorecluster and also take into account the available free space.


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

0 Kudos