Add a VMXNet3 NIC to VM attached to a vDS vPG

As part of an automated deployment of VMs I needed the ability to add a NIC that is attached to VMware vDS and attach it to a particular port group. Using code from LucD and using Project Onyx I cobbled together the following function to do just that.

# COMMENT: This function requires two arguments. The first being a VM object
#          and the second being a string matching the label of a VMware 
#          vNetwork Distributed Switch virtual port group. 
#          Note: Does *not* work for Nexus 1000V.
#
# Usage 1: Add-VMXNet3NIC-on-vDS <VM as [object]> <virtual port group name as [string]>
# Usage 2: Add-VMXNet3NIC-on-vDS (Get-VM -Name "MyVMName") "165_10.10.165.0_24"
# Uasge 3: $vm = Get-VM -Name "MyVMName"
#          $vpgname = "165_10.209.165.0_24"
#          Add-VMXNet3NIC-on-vDS $vm $vpgname
#
###################################################

Function Add-VMXNet3NIC-on-vDS
{
	
  param (
    [VMware.VimAutomation.Client20.VirtualMachineImpl] $vm,
    [string] $dvPgName
  )
  
  Begin {
  	
  }
  
  Process {
  
    #Firstly check that the parameter that has been passed is a VM object
    if ( $vm -isnot [VMware.VimAutomation.Client20.VirtualMachineImpl] ) {
      Write-Output "Invalid input recieved: A Virtual Machine object is expected."
      continue
    } # end if
  
    $vmview = Get-View -viewtype VirtualMachine -filter @{"Name" = $vm.Name}
    Write-Output "Attaching $vm to vDS virtual port group $dvPgName"
  
    # Find the dvSwitch & dvPortGroup (From:  http://lucd.info/?p=515)
    $esx = Get-View -Id $vmview.Runtime.Host
    foreach($netMoRef in $esx.Network){
      if($netMoRef.Type -eq "DistributedVirtualPortGroup"){
        $net = Get-View -Id $netMoRef
        if($net.Name -eq $dvPgName){
          $dvPgKey = $net.MoRef.Value
          #dvportgroup-191
          $dvSwitchUuid = (Get-View -Id $net.Config.DistributedVirtualSwitch).Summary.Uuid
          #f3 40 20 50 15 c0 d1 8d-02 4d f8 b4 48 9b df 06
  
          # Now that have determined the dvsvpg exists using luc's code, I can now add a vmxnet3 NIC to the VM
          $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
          $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
          $spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
          $spec.deviceChange[0].operation = "add"
          $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualVmxnet3
          $spec.deviceChange[0].device.key = -100
          $spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo
          $spec.deviceChange[0].device.backing.port = New-Object VMware.Vim.DistributedVirtualSwitchPortConnection
          $spec.deviceChange[0].device.backing.port.switchUuid = $dvSwitchUuid
          $spec.deviceChange[0].device.backing.port.portgroupKey = $dvPgKey
          $spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
          $spec.deviceChange[0].device.connectable.startConnected = $true
          $spec.deviceChange[0].device.connectable.allowGuestControl = $true
          $spec.deviceChange[0].device.connectable.connected = $true
          $spec.deviceChange[0].device.controllerKey = 100
          $spec.deviceChange[0].device.addressType = "generated"
          $spec.deviceChange[0].device.wakeOnLanEnabled = $true
          $_this = Get-View -Id $vmview.MoRef
          $_this.ReconfigVM_Task($spec)
  
        } # end if
      } # end if
    } # end foreach
      
    if ($dvPgKey -eq $null) {
       Write-Output "Error: No vDS virtual port group was found called: $dvPgName"
    } # end if
    
    #Destroy all variables at completion
    $vm=$null
    $dvPgName=$null
    $vmview=$null
    $dvPgKey=$null
    $dvSwitchUuid=$null
    $spec=$null
    $_this=$null
      
  } # end Process

  End {
  	#Write-Output "Function Finished"
  } # end End

} # end function

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.