VMware Cloud Community
macdougall
Contributor
Contributor

Batch VM deployment multiple Network adapters - IP issues

I've been writing a script to deploy 100+ VMs using information from a CSV file. Each VM has 2 network adapters.

When I run the script everything executes as it should. The VMs are created, powered up and then the oscustomization begins through VMtools.

When the oscustomization finishes there are 2 problems:

#1 VMs based off of Template1 get the Name, Domain, Workgroup, etc. applied but not the networking portion: IP, Subnet, Gateway, etc.

#2 VMs based off of Template2 get everything applied including the networking portion, but the IPs are assigned randomly to different network adapters.

If I run the script multiple times (deleting the test VMs in between) IP1 may be applied to network adapter1 the first time and then IP1 will be applied to network adapter2 the second time, with no pattern.

Both templates are systems running windows 7 64 bit.

Any ideas what causing these issues?

Main portion of the script is below:

$custSpec = "win64spec"

#Create temporary customizationspecs for each VM type
$specClone = New-OSCustomizationSpec -Spec $custSpec -Type NonPersistent

#Import CSV file into a list
$systems = Import-CSV $csvfile -encoding UTF8

#Create a blank hashtable
$hashTable = @{}

#Create VM and Apply Cust Spec to each VM
Foreach($system in $systems){

#set dyanmic variables
$name = $system.name
$datastore = $system.datastore
$ip1 = $system.ip1
$subnet1 = $system.subnet1
$gateway1 = $system.gateway1
$ip2 = $system.ip2
$subnet2 = $system.subnet2
$gateway2 = $system.gateway2
$dns = $system.dns
$cluster = $system.cluster
$VMHost = Get-VMhost -name $system.vmhostname
$type = $system.type

#determine template to use based off of CSV table


if ($type -eq $Sys1name) {
  $template = $Sys1template
}

elseif ($type -eq $Sys2name) {
  $template = $Sys2template
}

elseif ($type -eq $Sys3name) {
  $template = $Sys3template
}

else {
  Write-host "Check for errors in Template name in the CSV file. Name should be one of the following: $Sys1name, $Sys2name, $Sys2name"
}

#Remove nic mappings from spec
$nicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec $specClone
Remove-OSCustomizationNicMapping -OSCustomizationNicMapping $nicMapping -Confirm:$false

#Update default NIC mapping of Spec
$specClone | New-OsCustomizationNicMapping -Ipmode UseStaticIP -IPAddress $ip1 -SubnetMask $subnet1 -DefaultGateway $gateway1 -Dns $dns -Position 1
$specClone | New-OsCustomizationNicMapping -Ipmode UseStaticIP -IPAddress $ip2 -SubnetMask $subnet2 -DefaultGateway $gateway2 -Dns $dns -Position 2

#Create VMs
$Hashtable[(New-VM -Name $name -Template $template -Datastore $datastore -Vmhost $VMHost -oscustomizationspec $specClone -erroraction silentlycontinue -RunAsync).Id] = $system
}

$runningTasks1 = $HashTable.count
while($runningTasks1 -gt 0){
Get-Task | % {
  if($HashTable.ContainsKey($_.Id) -and $_.State -eq "Success"){
   $vmsuccessname = $HashTable[$_.Id].name
   Start-VM $vmsuccessname | select name,powerstate,@{n="IPAddress";e={@($_.guest.IPAddress[0])}} | ft -auto
         $HashTable.Remove($_.Id)
         $runningTasks1--
   Write-Host "Starting VM"
      }
      elseif($HashTable.ContainsKey($_.Id) -and $_.State -eq "Error"){
   $vmerrorname = $HashTable[$_.Id].name
   Start-VM $vmerrorname | select name,powerstate,@{n="IPAddress";e={@($_.guest.IPAddress[0])}} | ft -auto
         $HashTable.Remove($_.Id)
         $runningTasks1--
   Write-Host "Starting VM with error"
      }
   }
Write-host "Waiting for VMs to be created..."
Start-Sleep -Seconds 30
}

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

I don't really have an explanation, without looking deeper inside the customization processes (you can have a look at the logs of the customization that are left behind).

I normally only use 1 vNIC in a template, deploy the new VM from there and use only 1 NIC in the OSCustomizationSpec.

All additional vNIC I add and configure once the OSCustomization is finished, and the VM is reachable over the network.

I know, no real solution, but at least a possible bypass.


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

Reply
0 Kudos
macdougall
Contributor
Contributor

Thanks for the quick response!

I'll look at the logs and see if anything jumps out.

Which method do you use to add additional vNICs when the oscustomization is finished?

Reply
0 Kudos
LucD
Leadership
Leadership

Depends on the guest OS.

For Windows VMs, and when they are reachable over the mgmt network, we tend to use remote PS sessions.

As an alternative there is the Invoke-VMScript cmdlet, for both Windows and Linux.


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

Reply
0 Kudos
macdougall
Contributor
Contributor

Just a quick update to this issue:

It seems that IP addresses, for VMs created from the template, are being correctly applied to the network adapters as seen in the vSphere Client window, but the network adapters that show up in the vSphere client are being randomly associated with the network adapters that show when logged into the guest VM.

This seems more like a strange vSphere issue than a powercli issue. I'll post in a different section to see if I can find an answer. Thanks again Luc.

Reply
0 Kudos
macdougall
Contributor
Contributor

Re: VM from template with 2 NICs - Random vNIC to vNIC association

It seems the random vNIC association was caused by another known issue that was resolved when I deleted the vNICs re-added them and applied a hotfix.

I've attached the other thread where I posted the issue in case anyone needs it. Thanks again to the community!

Reply
0 Kudos