Create Multiple Virtual Machines with Two Network Adapters

You can deploy multiple virtual machines with two network adapters each and configure each adapter to use specific network settings by applying a customization specification.

You can configure each virtual machine to have one network adapter attached to a public network and one network adapter attached to a private network. You can configure the network adapters on the public network to use static IP addresses and the network adapters on the private network to use DHCP.

Prerequisites

Verify that you have defined a list of static IP addresses in a CSV file.

Procedure

  1. Define the naming convention for the virtual machines.
    $vmNameTemplate = "VM-{0:D3}"
  2. Save the cluster in which the virtual machines should be created into a variable.
    $cluster = Get-Cluster MyCluster
  3. Save the template on which the virtual machines should be based into a variable.
    $template = Get-Template MyTemplate
  4. Create the virtual machines.
    $vmList = @()
    	
    for ($i = 1; $i –le 100; $i++) {
        $vmName = $vmNameTemplate –f $i
        $vmList += New-VMName $vmName –ResourcePool $cluster –Template $template
    }
  5. Save the static IP addresses from the stored CSV file into a variable.
    $staticIpList = Import-CSV C:\StaticIPs.csv
  6. Create the customization specification.
    $linuxSpec = New-OSCustomizationSpecName LinuxCustomization –Domain vmware.com –DnsServer "192.168.0.10", "192.168.0.20" –NamingScheme VM –OSType Linux –Type NonPersistent
  7. Apply the customization specification to each virtual machine.
    for ($i = 0; $i –lt $vmList.Count; $i++) {
        # Acquire a new static IP from the list
        $ip = $staticIpList[$i].IP
    
        # Remove any NIC mappings from the specification
        $nicMapping = Get-OSCustomizationNicMappingOSCustomizationSpec $linuxSpec
        Remove-OSCustomizationNicMappingOSCustomizationNicMapping $nicMapping –Confirm:$false
    
        # Retrieve the virtual machine’s network adapter attached to the public network named "Public"
        $publicNIC = $vmList[$i] | Get-NetworkAdapter | where {$_.NetworkName -eq "Public"}
    
        # Retrieve the virtual machine’s network adapter attached to the private network named "Private"
        $privateNIC = $vmList[$i] | Get-NetworkAdapter | where {$_.NetworkName -eq "Private"}
    
        # Create a NIC mapping for the "Public" NIC that should use static IP
        $linuxSpec | New-OSCustomizationNicMappingIpMode UseStaticIP –IpAddress $ip –SubnetMask "255.255.252.0" –DefaultGateway "192.168.0.1" –NetworkAdapterMac $publicNIC.MacAddress
    
        # Create a NIC mapping for the "Private" NIC that should use DHCP
        $linuxSpec | New-OSCustomizationNicMappingIpMode UseDhcp –NetworkAdapterMac $privateNIC.MacAddress
    
        # Apply the customization
        Set-VM –VM $vmList[$i] –OSCustomizationSpec $linuxSpec –Confirm:$false
    }