Monday, June 3, 2013

POWERSHELL: Managing a VM's NIC's ..Changing vSwitch, adding a NIC and attaching to a the new vSwitch.

This has now been added to a free powershell hyper-v manager project.

Changing a vSwitch
So we have a VM named Test1 which is plugged into the vSwitch "Prod Network" and we want to move it to the "TEST Network"
In powershell type
get-vmnetworkadapter -vmname "Test1" | connect-vmnetworkadapter -switchname "TEST Network"

you can run this to see your VM's and vswitches thay are attached to. NOTE: if a VM Name is listed multiple times it has more than one NIC.
get-vmnetworkadapter * | Select vmname,switchname,ipaddresses | sort "VMName" | format-table -autosize  

Adding a new NIC and adding that new NIC to a vSwitch.
I am assuming you have NIC(1) plugged into the "PROD NETWORK" vSwitch and in this case we are adding a new NIC(2) assigned to the "BACKUP Network" vSwitch.
You need to log onto the VM and rename the Current NIC(1) to "PROD NIC1". Once it is renamed you need to shutdown and power off the VM.
Now we are ready to add a new NIC(2) and attached it to the vSwitch "BACKUP Network"
In powershell type:
Add-VMNetworkAdapter –VMName "Test1" –Name “Backup NIC2” -SwitchName "BACKUP Network" 
Now you can start the VM: Start-VM "Test1"
Once it is booted you need to RDP to the Server and rename the new "Network Adapter" to “Backup NIC2”
Now you see both NIC's renamed

Now you need to click somewhere in the white area under your NIC's and press the ALT key, you will see a Toolbar appear, Click Advanced/Advanced Settings.

Now change the network order as desired, in this case "PROD Network" needs to come first. Click OK to save your changes.

No comments:

Post a Comment