Moving on from yesterdays script (which showed you how to create a private virtual network) let’s look at creating an internal virtual network via a script. An internal virtual network is one that is not connected to a physical network adapter – but allows virtual machines to communicate with each other and the parent partition. This is a fair bit more complicated than creating a private virtual network:
Yikes! That is a bunch of code. Let’s step through the logic involved here. The logic flow is as follows:
- Create a new virtual network switch with nothing connected (MSVM_VirtualSwitchManagementService.CreateSwitch)
- Create a new internal switch port on the new virtual network switch (MSVM_VirtualSwitchManagementService.CreateSwitchPort)
- You can think of this as making a new jack to connect a network cable to on the virtual switch. You need to do this for virtual machine and parent partition connections.
- Create a new internal ethernet port (MSVM_VirtualSwitchManagementService.CreateInternalEthernetPortDynamicMac)
- The internal ethernet port is the virtual network adapter that appears inside the parent partition for the internal virtual network. If you were to stop the script after creating the internal ethernet port you would see a disconnected virtual network adapter in the parent partition.
- Finally connect the internal ethernet port to the internal switch port on the new switch (MSVM_VirtualSwitchManagementService.ConnectSwitchPort)
When it is all done you have an internal virtual network as Hyper-V creates in its own UI. Some extra things to highlight here:
- 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.
- The friendly name for the internal ethernet port is what appears as the device name for the virtual network adapter in the parent partition (when you open up the network connection management UI under Windows). In these scripts I make it the same as the virtual network name – which is what the Hyper-V UI does – but you can make it anything you want.
- 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 internal switch port. I am doing this because it is never displayed in the UI and does not need to be unique.
- In these scripts I use “CreateInternalEthernetPortDynamicMac”. There is a plain “CreateInternalEthernetPort” where you specify your own MAC address – but most of the time it is just easiest to use dynamic MAC addresses.
- You need to provide a CIM_LanEndpoint object when calling ConnectSwitchPort. The best way to get this is to take the Msvm_InternalEthernetPort object and walk the association through CIM_DeviceSAPImplementation to Cim_LanEndpoint.
UPDATE: I have made a minor update to the VBScript as I discovered that I needed to reinitialize TypeLib each time, otherwise you just get the same GUID multiple times over.