{"id":210,"date":"2010-02-24T14:09:59","date_gmt":"2010-02-24T03:09:59","guid":{"rendered":"http:\/\/threemillion.net\/blog\/?p=210"},"modified":"2010-02-24T14:09:59","modified_gmt":"2010-02-24T03:09:59","slug":"add-a-vmxnet3-nic-to-the-same-vpg-as-the-first-nic","status":"publish","type":"post","link":"https:\/\/threemillion.net\/blog\/?p=210","title":{"rendered":"Add a VMXNet3 nic to the same vPG as the first NIC."},"content":{"rendered":"<p>This script adds a vmxnet3 nic to a VM to be on the same virtual Port Proup (vPG) as the first NIC attached to the VM.<br \/>\nThis came about because a team had created a lot of VMs, say 100, all using e1000 nics (Which is the default NIC when creating a Windows 2008 R2 VM from the New VM Wizard) and I needed to get them to change to the VMXNet3 NIC. As you can expect I did not jump over the moon at the idea of having to do this manually so wrote the following script to do it for me.<br \/>\nOne additional caveat is that these VM NICs were attached a VMware vNetwork Distributed Switch. I have not tested it against a Standard vSwitch.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Description: Adds an vmxnet3 nic to a vm and connects it to the same virtual\r\n#      port group on a vDS as the first nic on the vm.\r\n# Author: Herschelle42\r\n# Usage 1: Add-Additional-VMXNet3NIC.ps1 &lt;VM as [object]&gt;\r\n# Usage 2: Add-Additional-VMXNet3NIC.ps1 (Get-VM -Name &quot;MyVMName&quot;)\r\n# Usage 3: $vm = Get-VM -Name &quot;MyVMName&quot;\r\n#          Add-Additional-VMXNet3NIC.ps1 $vm&lt;\/code&gt;\r\n\r\nProcess {\r\n#Firstly check that the parameter that has been passed is a VM object\r\nif ( $_ -isnot [VMware.VimAutomation.Client20.VirtualMachineImpl] ) {\r\nWrite-Output &quot;Invalid input recieved: A Virtual Machine object is expected.&quot;\r\ncontinue\r\n} # end if\r\n\r\n$vmview = Get-View -viewtype VirtualMachine -filter @{&quot;Name&quot; = $_.Name}\r\nWrite-Output $_.Name\r\n\r\n#Get the vNetwork Port group the VM is currently attached to\r\n$dvPortGroup = $vmview.Network[0].Value\r\n#dvportgroup-191\r\n\r\n#Get the Switch UUID of the current Network interface.\r\nforeach ($item in $vmview.Config.Hardware.Device) {\r\nif($item.Backing.Port.PortgroupKey -eq $dvPortGroup) {\r\n$SwitchUUID=$item.Backing.Port.SwitchUuid\r\n#Write-output &quot;SwitchUUID: &quot;$SwitchUUID\r\n#f3 40 20 50 15 c0 d1 8d-02 4d f8 b4 48 9b df 06\r\n} # end if\r\n} # end foreach\r\n\r\n# Now add a new NIC of vmxnet3 to the same network.\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 = $SwitchUUID\r\n$spec.deviceChange[0].device.backing.port.portgroupKey = $dvPortGroup\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\r\n$_this = Get-View -Id $vmview.MoRef\r\n$_this.ReconfigVM_Task($spec)\r\n\r\n#Destroy all variables at completion\r\n$vm=$null\r\n$vmName=$null\r\n$vmview=$null\r\n$dvPortGroup=$null\r\n$SwitchUUID=$null\r\n$spec=$null\r\n$_this=$null\r\n$item=$null\r\n\r\nWrite-Output &quot;Script Finished&quot;\r\n\r\n} # end process\r\n<\/pre>\n<p>Something important to note that this will not move any IP address configuration from the e1000 to the vmxnet3 nic, you will need to do this inside the Guest O\/S. (That could be the subject of another post.)<\/p>\n<p>Another thing is that before you remove the old nic from the VM, ensure you have disabled and or uninstalled the e1000 nic from within the Windows Guest OS. This is for Windows guests anyway, otherwise you will end up with devices in device manager. And if you do not removed any fixed IP address from the e1000 you will not be able to assign that same IP address to the vmxnet3 nic as it will already &#8220;be in use&#8221; according to Windows. Just a trap I fell into myself.<\/p>\n<p>So for the Windows VMs the following basic steps are required:<br \/>\n1. Ran my script to add a vmxnet3 nic<br \/>\n2. Open a VI Client console window and within Windows change the old Nic to DHCP<br \/>\n3. Set new nic to the fixed ip address<br \/>\n4. disable old nic<br \/>\n5. Open device manager, uninstall the old e1000 nic<br \/>\n6. Remove the old nic from the VM.<\/p>\n<p>Of course if the VM(s) do not have a fixed IP and only use DHCP skip some of the steps. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script adds a vmxnet3 nic to a VM to be on the same virtual Port Proup (vPG) as the first NIC attached to the VM. This came about because <a href=\"https:\/\/threemillion.net\/blog\/?p=210\" 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":""},"categories":[8,18],"tags":[34],"_links":{"self":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/210"}],"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=210"}],"version-history":[{"count":7,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":242,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions\/242"}],"wp:attachment":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}