Automation

 View Only
Expand all | Collapse all

Create a VM from CSV File

  • 1.  Create a VM from CSV File

    Posted Jan 21, 2018 03:06 PM

    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



  • 2.  RE: Create a VM from CSV File

    Posted Jan 21, 2018 03:19 PM

    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



  • 3.  RE: Create a VM from CSV File

    Posted Jan 21, 2018 03:26 PM

    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?


  • 4.  RE: Create a VM from CSV File

    Posted Jan 21, 2018 03:46 PM

    Hi ,

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

    Yes I can update VMware tool in my template.



  • 5.  RE: Create a VM from CSV File

    Posted Jan 21, 2018 03:56 PM

    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.



  • 6.  RE: Create a VM from CSV File

    Posted Jan 21, 2018 04:02 PM

    Hi , we have window 2012 server .

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



  • 7.  RE: Create a VM from CSV File
    Best Answer

    Posted Jan 22, 2018 07:04 AM

    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

    }



  • 8.  RE: Create a VM from CSV File

    Posted Jan 22, 2018 08:00 AM

    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



  • 9.  RE: Create a VM from CSV File

    Posted Jan 22, 2018 08:09 AM

    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.



  • 10.  RE: Create a VM from CSV File

    Posted Jan 22, 2018 08:42 AM

    Thanks for help .  Really appreciate



  • 11.  RE: Create a VM from CSV File

    Posted Nov 26, 2020 10:42 AM

    Hi  

    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