option explicit
Dim HyperVServer
Dim SwitchFriendlyName
Dim TypeLib
Dim SwitchName
Dim InternalEthernetPortFriendlyName
Dim InternalSwitchPortFriendlyName
Dim InternalSwitchPortName
Dim InternalEthernetPortName
Dim ScopeofResidence
Dim WMIService
Dim VirtualSwitchManagementService
Dim Switch
Dim InternalSwitchPort
Dim InternalEthernetPort
Dim InternalLanEndPoint
Dim InParam
Dim OutParams
'Prompt for the Hyper-V Server to use
HyperVServer = InputBox("Specify the Hyper-V Server to create the internal virtual network switch:")
'Get friendly name for the internal virtual network switch (and the internal ethernet port)
SwitchFriendlyName = InputBox("Specify the name of the new internal virtual network switch:")
InternalEthernetPortFriendlyName = SwitchFriendlyName
'Set the friendly name for the internal switch port
InternalSwitchPortFriendlyName = "InternalSwitchPort"
'Generate GUIDs for the unique switch name, internal switch port name and internal ethernet port name
Set TypeLib = CreateObject("Scriptlet.TypeLib")
SwitchName = TypeLib.Guid
Set TypeLib = CreateObject("Scriptlet.TypeLib")
InternalSwitchPortName = TypeLib.Guid
Set TypeLib = CreateObject("Scriptlet.TypeLib")
InternalEthernetPortName = TypeLib.Guid
'Set scope of residence
ScopeofResidence = null
'Get an instance of the WMI Service in the virtualization namespace.
set WMIService = GetObject("winmgmts:\\" & HyperVServer & "\root\virtualization")
'Get the Msvm_VirtualSwitchManagementService object
set VirtualSwitchManagementService = WMIService.ExecQuery("select * from Msvm_VirtualSwitchManagementService").ItemIndex(0)
'Create a new virtual network switch
'Setup the input parameter list
set InParam = VirtualSwitchManagementService.Methods_("CreateSwitch").InParameters.SpawnInstance_()
InParam.FriendlyName = SwitchFriendlyName
InParam.Name = SwitchName
InParam.NumLearnableAddresses = 1024
InParam.ScopeofResidence = ScopeofResidence
'Execute the method and store the results in OutParam
set OutParams = VirtualSwitchManagementService.ExecMethod_("CreateSwitch", InParam)
'Get the new switch object out of the results
Set Switch = WMIService.Get(OutParams.CreatedVirtualSwitch)
'Create a new internal switch port
'Setup the input parameter list
set InParam = VirtualSwitchManagementService.Methods_("CreateSwitchPort").InParameters.SpawnInstance_()
InParam.VirtualSwitch = Switch.Path_.Path
InParam.FriendlyName = InternalSwitchPortFriendlyName
InParam.Name = InternalSwitchPortName
InParam.ScopeofResidence = ScopeofResidence
'Execute the method and store the results in OutParam
set OutParams = VirtualSwitchManagementService.ExecMethod_("CreateSwitchPort", InParam)
'Get the new internal switch port out of the results
Set InternalSwitchPort = WMIService.Get(OutParams.CreatedSwitchPort)
'Create a new internal ethernet port
'Setup the input parameter list
set InParam = VirtualSwitchManagementService.Methods_("CreateInternalEthernetPortDynamicMac").InParameters.SpawnInstance_()
InParam.FriendlyName = InternalEthernetPortFriendlyName
InParam.Name = InternalEthernetPortName
'Execute the method and store the results in OutParam
set OutParams = VirtualSwitchManagementService.ExecMethod_("CreateInternalEthernetPortDynamicMac", InParam)
'Get the new internal ethernet port out of the results
Set InternalEthernetPort = WMIService.Get(OutParams.CreatedInternalEthernetPort)
'Get the CIM_LanEndpoint Object associated with the internal ethernet port
Set InternalLanEndPoint = (InternalEthernetPort.Associators_("CIM_DeviceSAPImplementation", "Cim_LanEndpoint")).ItemIndex(0)
'Connect the switch to the internal ethernet port via the CIM_LanEndpoint
'Setup the input parameter list
set InParam = VirtualSwitchManagementService.Methods_("ConnectSwitchPort").InParameters.SpawnInstance_()
InParam.LANEndPoint = InternalLanEndPoint.Path_.Path
InParam.SwitchPort = InternalSwitchPort.Path_.Path
'Execute the method and store the results in OutParam
set OutParams = VirtualSwitchManagementService.ExecMethod_("ConnectSwitchPort", InParam)