{"id":208,"date":"2010-02-15T21:52:24","date_gmt":"2010-02-15T10:52:24","guid":{"rendered":"http:\/\/threemillion.net\/blog\/?p=208"},"modified":"2013-04-14T17:17:55","modified_gmt":"2013-04-14T07:17:55","slug":"add-a-vmxnet3-nic-to-vm-attached-to-a-vds-vpg","status":"publish","type":"post","link":"https:\/\/threemillion.net\/blog\/?p=208","title":{"rendered":"Add a VMXNet3 NIC to VM attached to a vDS vPG"},"content":{"rendered":"<p>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 <a href=\"http:\/\/lucd.info\">LucD<\/a> and using <a href=\"http:\/\/communities.vmware.com\/docs\/DOC-11829\">Project Onyx<\/a> I cobbled together the following function to do just that.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# COMMENT: This function requires two arguments. The first being a VM object\r\n#          and the second being a string matching the label of a VMware \r\n#          vNetwork Distributed Switch virtual port group. \r\n#          Note: Does *not* work for Nexus 1000V.\r\n#\r\n# Usage 1: Add-VMXNet3NIC-on-vDS &lt;VM as [object]&gt; &lt;virtual port group name as [string]&gt;\r\n# Usage 2: Add-VMXNet3NIC-on-vDS (Get-VM -Name &quot;MyVMName&quot;) &quot;165_10.10.165.0_24&quot;\r\n# Uasge 3: $vm = Get-VM -Name &quot;MyVMName&quot;\r\n#          $vpgname = &quot;165_10.209.165.0_24&quot;\r\n#          Add-VMXNet3NIC-on-vDS $vm $vpgname\r\n#\r\n###################################################\r\n\r\nFunction Add-VMXNet3NIC-on-vDS\r\n{\r\n\t\r\n  param (\r\n    [VMware.VimAutomation.Client20.VirtualMachineImpl] $vm,\r\n    [string] $dvPgName\r\n  )\r\n  \r\n  Begin {\r\n  \t\r\n  }\r\n  \r\n  Process {\r\n  \r\n    #Firstly check that the parameter that has been passed is a VM object\r\n    if ( $vm -isnot [VMware.VimAutomation.Client20.VirtualMachineImpl] ) {\r\n      Write-Output &quot;Invalid input recieved: A Virtual Machine object is expected.&quot;\r\n      continue\r\n    } # end if\r\n  \r\n    $vmview = Get-View -viewtype VirtualMachine -filter @{&quot;Name&quot; = $vm.Name}\r\n    Write-Output &quot;Attaching $vm to vDS virtual port group $dvPgName&quot;\r\n  \r\n    # Find the dvSwitch &amp; dvPortGroup (From:  http:\/\/lucd.info\/?p=515)\r\n    $esx = Get-View -Id $vmview.Runtime.Host\r\n    foreach($netMoRef in $esx.Network){\r\n      if($netMoRef.Type -eq &quot;DistributedVirtualPortGroup&quot;){\r\n        $net = Get-View -Id $netMoRef\r\n        if($net.Name -eq $dvPgName){\r\n          $dvPgKey = $net.MoRef.Value\r\n          #dvportgroup-191\r\n          $dvSwitchUuid = (Get-View -Id $net.Config.DistributedVirtualSwitch).Summary.Uuid\r\n          #f3 40 20 50 15 c0 d1 8d-02 4d f8 b4 48 9b df 06\r\n  \r\n          # Now that have determined the dvsvpg exists using luc's code, I can now add a vmxnet3 NIC to the VM\r\n          $spec = New-Object VMware.Vim.VirtualMachineConfigSpec\r\n          $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)\r\n          $spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec\r\n          $spec.deviceChange[0].operation = &quot;add&quot;\r\n          $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualVmxnet3\r\n          $spec.deviceChange[0].device.key = -100\r\n          $spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualEthernetCardDistributedVirtualPortBackingInfo\r\n          $spec.deviceChange[0].device.backing.port = New-Object VMware.Vim.DistributedVirtualSwitchPortConnection\r\n          $spec.deviceChange[0].device.backing.port.switchUuid = $dvSwitchUuid\r\n          $spec.deviceChange[0].device.backing.port.portgroupKey = $dvPgKey\r\n          $spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo\r\n          $spec.deviceChange[0].device.connectable.startConnected = $true\r\n          $spec.deviceChange[0].device.connectable.allowGuestControl = $true\r\n          $spec.deviceChange[0].device.connectable.connected = $true\r\n          $spec.deviceChange[0].device.controllerKey = 100\r\n          $spec.deviceChange[0].device.addressType = &quot;generated&quot;\r\n          $spec.deviceChange[0].device.wakeOnLanEnabled = $true\r\n          $_this = Get-View -Id $vmview.MoRef\r\n          $_this.ReconfigVM_Task($spec)\r\n  \r\n        } # end if\r\n      } # end if\r\n    } # end foreach\r\n      \r\n    if ($dvPgKey -eq $null) {\r\n       Write-Output &quot;Error: No vDS virtual port group was found called: $dvPgName&quot;\r\n    } # end if\r\n    \r\n    #Destroy all variables at completion\r\n    $vm=$null\r\n    $dvPgName=$null\r\n    $vmview=$null\r\n    $dvPgKey=$null\r\n    $dvSwitchUuid=$null\r\n    $spec=$null\r\n    $_this=$null\r\n      \r\n  } # end Process\r\n\r\n  End {\r\n  \t#Write-Output &quot;Function Finished&quot;\r\n  } # end End\r\n\r\n} # end function\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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. <a href=\"https:\/\/threemillion.net\/blog\/?p=208\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[8,18],"tags":[30,33,34,31,32],"class_list":["entry","author-admin","post-208","post","type-post","status-publish","format-standard","category-powershell","category-vmware-virtualisation","tag-vds","tag-virtual-port-group","tag-vmxnet3","tag-vnetwork-distributed-switch","tag-vpg"],"_links":{"self":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=208"}],"version-history":[{"count":7,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":422,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions\/422"}],"wp:attachment":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}