Solved

Script for building VMs

  • 12 February 2020
  • 6 replies
  • 2159 views

  • Adventurer
  • 3 replies

Can someone help me with a power shell script which can help us to build multiple VMs by using a Win10 template we have.

The environment that I am trying to build the VDIs have Nutanix as the Hardware and ESX 6.5 as the Hypervisor. The script should able to build the machines with sequential number and put them under a specif folder in Hypervisor. I can get the different templates for different containers and with proper Vlan.

icon

Best answer by HPN 12 February 2020, 23:17

Thanks Dan. So if I got it correct i need to create a CSV file in the below format

Machine Name

EsxHost

Datastore

Template

Network


And then use the script to consume the above CSV file to build the machines. Need to provide the VLAN information in network field and container information in Datastore field

$fileName = 'whatever.csv'

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

$vm = New-VM -Name $vmLine.Server -VMHost $vmLine.EsxHost -Datastore $vmLine.Datastore -Template $vmLine.Template

Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $vmLine.Network -StartConnected

}

 

View original

This topic has been closed for comments

6 replies

Userlevel 1
Badge +1

Hi @HPN 

I am still working on this one but am glad to share what I have so far to assist you:

Link to CSV for creating multiple VM’s

https://drive.google.com/file/d/1yvxXVAc7mn4NHrZmv02boTb4lVhV4JJK/view?usp=sharing

https://github.com/cloudcor-ntnx/ntnx-ps-advanced/blob/master/new-vm-from-csv.ps1

It essentially loads a csv files with the values necessary and then does a foreach loop on each object in the CSV. 

Hi @danny_sre 

Thanks for your quick response. Can you please allow my gmail ID: hara.nayak@gmail.com access to the google drive as you mentioned above?

For running this script do we need to have special permission in vmware or Nutanix environment. I have the full Admin access on Hypervisor level but read only access in Nutanix.

Userlevel 1
Badge +1

@HPN 

Based off your response I would then recommend writing the script for PowerCLI and connect to the esxi part of the environment for creating the new VM’s

https://code.vmware.com/forums/2530/vsphere-powercli#580519

$fileName = 'whatever.csv'

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

$vm = New-VM -Name $vmLine.Server -VMHost $vmLine.EsxHost -Datastore $vmLine.Datastore -Template $vmLine.Template

Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $vmLine.Network -StartConnected

}

 

Hi @danny_sre 

I checked the link but how we can control the number of builds getting to a specific container?

 

Userlevel 1
Badge +1

@HPN 

The containers are mapped to the esxi host as datastores, so you would simply provide a container name that correlates to the esxi datastore:

Datastore $vmLine.Datastore

Thanks Dan. So if I got it correct i need to create a CSV file in the below format

Machine Name

EsxHost

Datastore

Template

Network


And then use the script to consume the above CSV file to build the machines. Need to provide the VLAN information in network field and container information in Datastore field

$fileName = 'whatever.csv'

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

$vm = New-VM -Name $vmLine.Server -VMHost $vmLine.EsxHost -Datastore $vmLine.Datastore -Template $vmLine.Template

Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $vmLine.Network -StartConnected

}