VMware Cloud Community
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Create a VM from CSV File

Hi All,

I am looking for a script to create multiple VM or single VM using details from CSV file. Below are the my CSV file details.

ServerName

EsxHost

Datastore

Template

Network

IPAddress

Gateway

FirstDNS

SecondDNS

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could start from something like this (note that you probably have to fine-tune this a bit).

This assumes that your current credentials have admin authority inside the guest OS of these VMs.

If not, you will have to provide a GuestCredential on the Invoke-VMScript cmdlet.

You will have to check the netsh commands to verify that they have a valid syntax.

$fileName = 'whatever.csv'

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

    # Create VM

    $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 -Confirm:$false

    # Wait till VM is running

    Start-VM -VM $vm

    while($vm.Guest.State -ne 'running'){

        $vm = Get-VM -Name $vm.Name

        sleep 5

    }

    # Configure guest OS network

    $netsh = "c:\windows\system32\netsh.exe interface ip set address ""Local Area Connection"" static $($vmLine.IPAddress) $($vmLine.NetMask) $($vmLine.Gateway) 1"

    Invoke-VMScript -VM $VM -ScriptType bat -ScriptText $netsh

    $netsh = "c:\windows\system32\netsh.exe interface ip set dns ""Local Area Connection"" static $($vmLine.FirstDNS) $($vmLine.SecondDns)"

    Invoke-VMScript -VM $VM -ScriptType bat -ScriptText $netsh

}


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

View solution in original post

Reply
0 Kudos
10 Replies
admin
Immortal
Immortal
Jump to solution

I found below community link  for create VM from Script . It is old post but I hope it would help you ...

Create Multiple VMs across multiple Vsphere using spreadsheet V2

Regards,

Randhir

If you found my answers useful please consider marking them as Correct OR Helpful

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are 2 parts to this.

First creating the VMs, which can be done with the New-VM cmdlet.

Something like this

$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

}

For the second part, the configuration of the network inside the guest OS, you have two options.

  • Use an OSCustomizationSpec with the New-VM
  • Use Invoke-VMScript to configure the network once the VM is running. Note that this requires VMware Tools to be installed.

Some questions:

  • Are you using OSCustomizationSpecs with your templates?
  • Are the VMware Tools installed in your templates?


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

Reply
0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Hi ,

Thanks for your response. I am not using any customisation spec as of now

Yes I can update VMware tool in my template.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then it depends which type of guest OS you have running inside the VM.

Do you have the instructions to set up networking for that guest OS?

You can run them inside the guest OS through the Invoke-VMScript cmdlet.

If the guest is for example a Windows OS, you could use Alan's POWERCLI: CHANGING A VM IP ADDRESS WITH INVOKE-VMSCRIPT post.


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

Reply
0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Hi , we have window 2012 server .

Can you please club both part and give it to me . Sorry to asking more

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could start from something like this (note that you probably have to fine-tune this a bit).

This assumes that your current credentials have admin authority inside the guest OS of these VMs.

If not, you will have to provide a GuestCredential on the Invoke-VMScript cmdlet.

You will have to check the netsh commands to verify that they have a valid syntax.

$fileName = 'whatever.csv'

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

    # Create VM

    $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 -Confirm:$false

    # Wait till VM is running

    Start-VM -VM $vm

    while($vm.Guest.State -ne 'running'){

        $vm = Get-VM -Name $vm.Name

        sleep 5

    }

    # Configure guest OS network

    $netsh = "c:\windows\system32\netsh.exe interface ip set address ""Local Area Connection"" static $($vmLine.IPAddress) $($vmLine.NetMask) $($vmLine.Gateway) 1"

    Invoke-VMScript -VM $VM -ScriptType bat -ScriptText $netsh

    $netsh = "c:\windows\system32\netsh.exe interface ip set dns ""Local Area Connection"" static $($vmLine.FirstDNS) $($vmLine.SecondDns)"

    Invoke-VMScript -VM $VM -ScriptType bat -ScriptText $netsh

}


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

Reply
0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Hi ,

Thanks for your response , what if i use static value with out file. There are two scripts listed below , can you confirm that are these correct or not if not can you correct

$oscust = New-OSCustomizationSpec -Name temp -Type NonPersistent -OSCustomizationSpec teplatesc |

Get-OSCustomizationNicMapping |

Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 123.45.67.68 -SubnetMask 255.255.255.0 -DefaultGateway 123.45.67.99

New-VM -Name 'TestVM' -OSCustomizationSpec $osCust -Template –VMHost 'VMHost-1' -Datastore 'TestDatastore' -DiskGB 40 -MemoryGB 8 -NumCpu 2 -NetworkName 'mylan'

======================*********************************==================================

Get-OSCustomizationNicMapping |

Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 123.45.67.68 -SubnetMask 255.255.255.0 -DefaultGateway 123.45.67.99

New-VM -Template $template -Name MyVM -OSCustomizationSpec $osCust -VMHost $esx -Datastore $ds -DiskStorageFormat Thin |

Set-VM -NumCpu 2 -MemoryGB 4 -Confirm:$false |

Start-VM -Confirm:$false

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The 1st script you listed is nearly complete, the 2nd one definitely isn't.

In the 1st script, it creates a temporary OSCustomizationSpec, and then sets the guest OS network config in the OSCustomizationNicMapping.

Note that you can also configure the DNS servers with the DnsServer parameter on the on the New-OSCustomizationSpec cmdlet.


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

Reply
0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Thanks for help .  Really appreciate

Reply
0 Kudos
charmse
Contributor
Contributor
Jump to solution

Hi @LucD 

I would like create several VM from excel file with these parameter, but i'm dummy in this automatization; the input csv file contains several entries but my problem are in few cell there no information, by exemple:

Host   Num   zone DRS    IP1 MASK1 GWAD VLName   VLID    IP2      ... TEMPLATE     VMFOLDER           CPU RAM   DISK1(GB) DISK2(GB)
SRV1 empty  PUB empty  IP1 MASK1 GW1   VLName         ID   empty  ...   OSWIN         /CLUSTER/APP1     1     4       60                  "empty"
SRV2 empty  PUB empty  IP1 MASK1 GW1   VLName         ID    IP2      ...   OSLIN          /CLUSTER/APP2     2      4       10                  20

How to do these ? Could you please help me ?

Thank you by advance

 

Reply
0 Kudos