Continuing the vbscripting for Hyper-V, here’s a another one.
This is about creating a virtual network and “assigning” it the physical network card.
Here it goes:
Option explicit
Dim HyperVServer
Dim SwitchFriendlyName
Dim TypeLib
Dim SwitchName
Dim InternalEthernetPortFriendlyName
Dim InternalSwitchPortFriendlyName
Dim InternalSwitchPortName
Dim InternalEthernetPortName
Dim ExternalSwitchPortFriendlyName
Dim ExternalSwitchPortName
Dim ExternalEthernetPort
Dim ExternalEthernetPorts
Dim ExternalEthernetPortName
Dim ExternalEthernetPortString
Dim ScopeofResidence
Dim WMIService
Dim VirtualSwitchManagementService
Dim Switch
Dim InternalSwitchPort
Dim ExternalSwitchPort
Dim InParam
Dim OutParams
Dim Job
Dim Test
‘Prompt for the Hyper-V Server to use
HyperVServer = InputBox("Specify the Hyper-V Server to create the internal virtual network switch:")
‘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)
‘Get all possible physical network adapters that can be used for a switch
Set ExternalEthernetPorts = WMIService.ExecQuery("Select * From Msvm_ExternalEthernetPort WHERE IsBound=False AND EnabledState=2")
‘Build the string to ask what physical network adapter to use
ExternalEthernetPortString = "The following network adapters are available:" & chr(10) & chr(10)
for each ExternalEthernetPort in ExternalEthernetPorts
ExternalEthernetPortString = ExternalEthernetPortString & ExternalEthernetPort.Name & chr(10)
next
ExternalEthernetPortString = ExternalEthernetPortString & chr(10) & "Please type in the (exact) name of the network adapter you want to use for the external connection:"
‘Setup a loop to keep on asking for a network adapter until we get a valid response
Test = true
while test
ExternalEthernetPortName = InputBox(ExternalEthernetPortString)
on error resume next
Set ExternalEthernetPort = WMIService.ExecQuery("Select * From Msvm_ExternalEthernetPort WHERE Name=’" & ExternalEthernetPortName & "’ AND IsBound=False AND EnabledState=2").ItemIndex(0)
If Err.Number = 0 Then
Test = false
End If
on error goto 0
Wend
‘Get friendly name for the external virtual network switch (and the internal ethernet port)
SwitchFriendlyName = InputBox("Specify the name of the new external virtual network switch:")
InternalEthernetPortFriendlyName = SwitchFriendlyName
‘Set the friendly name for the internal switch port and external switch port
InternalSwitchPortFriendlyName = "InternalSwitchPort"
ExternalSwitchPortFriendlyName = "ExternalSwitchPort"
‘Generate GUIDs for the unique switch name, internal switch port name, internal ethernet port name and external switch 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 TypeLib = CreateObject("Scriptlet.TypeLib")
ExternalSwitchPortName = TypeLib.Guid
‘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 = null
‘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 = null
‘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 external switch port
‘Setup the input parameter list
set InParam = VirtualSwitchManagementService.Methods_("CreateSwitchPort").InParameters.SpawnInstance_()
InParam.VirtualSwitch = Switch.Path_.Path
InParam.FriendlyName = ExternalSwitchPortFriendlyName
InParam.Name = ExternalSwitchPortName
InParam.ScopeofResidence = null
‘Execute the method and store the results in OutParam
set OutParams = VirtualSwitchManagementService.ExecMethod_("CreateSwitchPort", InParam)
‘Get the new external switch port out of the results
Set ExternalSwitchPort = WMIService.Get(OutParams.CreatedSwitchPort)
‘Pull it all together with a call to SetupSwitch
‘Setup the input parameter list
set InParam = VirtualSwitchManagementService.Methods_("SetupSwitch").InParameters.SpawnInstance_()
InParam.ExternalSwitchPort = ExternalSwitchPort.Path_.Path
InParam.InternalSwitchPort = InternalSwitchPort.Path_.Path
InParam.ExternalEthernetPort = ExternalEthernetPort.Path_.Path
InParam.InternalEthernetPortName = InternalEthernetPortName
InParam.InternalEthernetPortFriendlyName = InternalEthernetPortFriendlyName
‘Execute the method and store the results in OutParams
set OutParams = VirtualSwitchManagementService.ExecMethod_("SetupSwitch", InParam)
‘Check to see if the job completed synchronously
if (OutParams.ReturnValue = 0) then
Wscript.Echo "External virtual network created."
elseif (OutParams.ReturnValue <> 4096) Then
Wscript.Echo "Failed to create external virtual network." & OutParams.ReturnValue
else
‘Get the job object
set Job = WMIService.Get(OutParams.Job)
‘Wait for the job to complete (3 == starting, 4 == running)
while (Job.JobState = 3) or (Job.JobState = 4)
Wscript.Echo "Creating virtual network. " & Job.PercentComplete & "% complete"
WScript.Sleep(10000)
‘Refresh the job object
set Job = WMIService.Get(OutParams.Job)
Wend
‘Provide details if the job fails (7 == complete)
MsgBox (Job.JobState)
if (Job.JobState <> 7) Then
Wscript.Echo "External virtual network created."
Wscript.Echo "ErrorCode:" & Job.ErrorCode
Wscript.Echo "ErrorDescription:" & Job.ErrorDescription
Else
Wscript.Echo "Failed to create external virtual network."
Wscript.Echo "ErrorCode:" & Job.ErrorCode
end If
end If
No comments:
Post a Comment