Script: Creating an internal virtual network with Hyper-V – Plus…
Almost two weeks ago I posted a script for creating an internal virtual network, and today I would like to revisit it and extend it. As I prefer to use core installations of Windows Server whenever possible – but this can make it a pain to configure the IP address on the internal network interface of an internal virtual network. So here is an updated script that not only creates an internal virtual network but also configures the IP address of the internal network interface.
VBScript:
option explicit
Dim HyperVServer
Dim SwitchFriendlyName
Dim TypeLib
Dim SwitchName
Dim InternalEthernetPortFriendlyName
Dim InternalSwitchPortFriendlyName
Dim InternalSwitchPortName
Dim InternalEthernetPortName
Dim ScopeofResidence
Dim WMIService
Dim Cimv2WMIService
Dim VirtualSwitchManagementService
Dim Switch
Dim InternalSwitchPort
Dim InternalEthernetPort
Dim InternalLanEndPoint
Dim InternalEthernetPortIP
Dim InternalEthernetPortSubnetMask
Dim InternalEthernetPortGateway
Dim Query
Dim NetworkAdapterConfiguration
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
'Get IP Address for the internal ethernet port
InternalEthernetPortIP = InputBox("Specify the IP address for the internal interface on the virtual network (e.g. 10.0.0.10):")
'Get subnet mask for the internal ethernet port
InternalEthernetPortSubnetMask = InputBox("Specify the subnet mask for the internal interface on the virtual network (e.g. 255.0.0.0):")
'Get gateway Address for the internal ethernet port
InternalEthernetPortGateway = InputBox("Specify the gateway address for the internal interface on the virtual network (e.g. 10.0.0.1):")
'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 an instance of the WMI Service in the CimV2 namespace.
set Cimv2WMIService = GetObject("winmgmts:\\" & HyperVServer & "\root\CimV2")
'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)
'Find the Win32_NetworkAdapterConfiguration object associated with the InternalEthernetPort
Query = "select * from Win32_NetworkAdapterConfiguration WHERE SettingID='" & InternalEthernetPort.DeviceID & "'"
set NetworkAdapterConfiguration = Cimv2WMIService.ExecQuery(Query).ItemIndex(0)
'Call EnableStatic to set the IP address and subnet mask
NetworkAdapterConfiguration.EnableStatic Array(InternalEthernetPortIP), Array(InternalEthernetPortSubnetMask)
'Call SetGateways to set the gateway IP address
NetworkAdapterConfiguration.SetGateways Array(InternalEthernetPortGateway)
PowerShell:
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Get friendly name for the internal virtual network switch (and internal ethernet port)
$SwitchFriendlyName = Read-Host "Specify the name of the new internal virtual network switch"
$InternalEthernetPortFriendlyName = $SwitchFriendlyName
# Get IP Address for the internal ethernet port
$InternalEthernetPortIP = Read-Host "Specify the IP address for the internal interface on the virtual network (e.g. 10.0.0.10)"
# Get subnet mask for the internal ethernet port
$InternalEthernetPortSubnetMask = Read-Host "Specify the subnet mask for the internal interface on the virtual network (e.g. 255.0.0.0)"
# Get gateway Address for the internal ethernet port
$InternalEthernetPortGateway = Read-Host "Specify the gateway address for the internal interface on the virtual network (e.g. 10.0.0.1)"
# 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
$SwitchName = [guid]::NewGuid().ToString()
$InternalSwitchPortName = [guid]::NewGuid().ToString()
$InternalEthernetPortName = [guid]::NewGuid().ToString()
# Setup some other values that will be used
$NumLearnableAddresses = 1024
$ScopeOfResidence = ""
# Get the Msvm_VirtualSwitchManagementService WMI Object on the system we are going to be working with
$VirtualSwitchManagementService = gwmi Msvm_VirtualSwitchManagementService -namespace "root\virtualization" -computername $HyperVServer
# Create a new switch with 1024 learnable addresses
$Result = $VirtualSwitchManagementService.CreateSwitch($SwitchName, $SwitchFriendlyName, $NumLearnableAddresses, $ScopeOfResidence)
# Get the WMI object for the new switch out of the results
$Switch = [WMI]$Result.CreatedVirtualSwitch
# Create Internal Switch Port
$Result = $VirtualSwitchManagementService.CreateSwitchPort($Switch, $InternalSwitchPortName, $InternalSwitchPortFriendlyName, $ScopeOfResidence)
# Get the WMI object for the new switch port out of the results
$InternalSwitchPort = [WMI]$Result.CreatedSwitchPort
# Create Internal Ethernet Port
$Result = $VirtualSwitchManagementService.CreateInternalEthernetPortDynamicMac($InternalEthernetPortName, $InternalEthernetPortFriendlyName)
# Get the WMI object for the new ethernet port out of the results
$InternalEthernetPort = [WMI]$Result.CreatedInternalEthernetPort
# Get the CIM_LanEndpoint Object associated with the internal ethernet port
$query = "Associators of {$InternalEthernetPort} Where ResultClass=CIM_LanEndpoint"
$InternalLanEndPoint = gwmi -namespace root\virtualization -query $query -computername $HyperVServer
# Connect the internal ethernet port to the virtual network switch
$Result = $VirtualSwitchManagementService.ConnectSwitchPort($InternalSwitchPort, $InternalLanEndPoint)
# Find the Win32_NetworkAdapterConfiguration object associated with the InternalEthernetPort
$filter = "SettingID='" + $InternalEthernetPort.DeviceID +"'"
$NetworkAdapterConfiguration = gwmi Win32_NetworkAdapterConfiguration -filter $filter -computername $HyperVServer
# Call EnableStatic to set the IP address and subnet mask
$Result = $NetworkAdapterConfiguration.EnableStatic($InternalEthernetPortIP, $InternalEthernetPortSubnetMask)
# Call SetGateways to set the gateway IP address
$Result = $NetworkAdapterConfiguration.SetGateways($InternalEthernetPortGateway)
The way these scripts work is that they use the “DeviceID” on the MSVM_InternalEthernetPort to find the related Win32_NetworkAdapterConfiguration object. They then call EnableStatic and SetGateways to configure the IP address.
Cheers,
Ben