Okay, last script for the week. Creating an external-only virtual network (one without a connection in the parent partition).
Going through the flow again:
- First we iterate over all instances of Msvm_ExternalEthernetPort to let the user know what network adapters are available
- Then we create a new virtual network (MSVM_VirtualSwitchManagementService.CreateSwitch)
- Next we create an external switch port (MSVM_VirtualSwitchManagementService.CreateSwitchPort)
- This is making a jack on the virtual network switch to connect the physical adapter to
- Prepare the physical network adapter to be connected to the virtual network switch (Msvm_VirtualSwitchManagementService.BindExternalEthernetPort)
- Finally connect the external ethernet port to the external switch port on the new switch (MSVM_VirtualSwitchManagementService.ConnectSwitchPort)
Some interesting points to make:
- When looking at physical network adapters (MSVM_ExternalEthernetPort):
- Network adapters that are already connected to a virtual network will return “true” for “IsBound”
- If you do not check the EnabledState you will see entries for old network adapters that are no longer present in the physical computer
- You really should be using the “DeviceID” to uniquely identify network adapters, as the name is not guaranteed to be unique – but as no one knows the device ID, and the chances of a duplicate name are incredibly low – I prefer to use the name
- Since I am asking the user to type in a long string for the network name – I have put in a loop so that if they make a mistake they have a chance to enter it again.
- All of the above methods return a result object that contains a reference to the WMI object that has been created. You will see in the code that after each method call we then need to go and grab this object out of the results.
- The results also contain a return code that indicates success or failure. For brevity of code I am not checking this value and am assuming success on each of these calls – however for correctness you should be checking this value.
- For all the non-friendly names I am generating GUIDs. This is not required but is strongly recommended. These values are never displayed in the UI and are used for internal purposes and *must* be unique.
- I have hardcoded the value for the friendly name of the external switch port. I am doing this because it is never displayed in the UI and does not need to be unique.
- You need to provide a CIM_LanEndpoint object when calling ConnectSwitchPort. The best way to get this is to take the Msvm_ExternalEthernetPort object and walk the association through CIM_DeviceSAPImplementation to Cim_LanEndpoint.
- This only works after calling BindExternalEthernetPort
- You should be careful with this script as it will render the parent partition unable to use the physical network adapter that you select.