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
-
Define the naming
convention for the virtual machines.
$vmNameTemplate = "VM-{0:D3}"
-
Save the cluster in
which the virtual machines should be created into a variable.
$cluster = Get-Cluster MyCluster
-
Save the template on
which the virtual machines should be based into a variable.
$template = Get-Template MyTemplate
-
Create the virtual
machines.
$vmList = @()
for ($i = 1; $i –le 100; $i++) {
$vmName = $vmNameTemplate –f $i
$vmList += New-VM –Name $vmName –ResourcePool $cluster –Template $template
}
-
Save the static IP
addresses from the stored CSV file into a variable.
$staticIpList = Import-CSV C:\StaticIPs.csv
-
Create the customization
specification.
$linuxSpec = New-OSCustomizationSpec –Name LinuxCustomization –Domain vmware.com –DnsServer "192.168.0.10", "192.168.0.20" –NamingScheme VM –OSType Linux –Type NonPersistent
-
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-OSCustomizationNicMapping –OSCustomizationSpec $linuxSpec
Remove-OSCustomizationNicMapping –OSCustomizationNicMapping $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-OSCustomizationNicMapping –IpMode 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-OSCustomizationNicMapping –IpMode UseDhcp –NetworkAdapterMac $privateNIC.MacAddress
# Apply the customization
Set-VM –VM $vmList[$i] –OSCustomizationSpec $linuxSpec –Confirm:$false
}