1 out of 1 rated this helpful - Rate this topic

CreateInternalEthernetPortDynamicMac method of the Msvm_VirtualSwitchManagementService Class

Hyper-V

Creates a new internal Ethernet port. It's almost the same as CreateInternalEthernetPort expect for the new internal Ethernet port's MAC address is created dynamically instead of being passed as a parameter.

Syntax

uint32 CreateInternalEthernetPortDynamicMac(
  [in]   string Name,
  [in]   string FriendlyName,
  [out]  Msvm_InternalEthernetPort REF CreatedInternalEthernetPort
);

Parameters

Name [in]

Type: string

The name of the port. This name must be unique.

FriendlyName [in]

Type: string

A user-readable name for the port.

CreatedInternalEthernetPort [out]

Type: Msvm_InternalEthernetPort

Upon successful completion of this method, this parameter contains the created switch port. See Msvm_InternalEthernetPort.

Return value

Type: uint32

The method returns 0 if it succeeded synchronously. Any other return value indicates an error.

Remarks

Access to the Msvm_VirtualSwitchManagementService class might be restricted by UAC Filtering. For more information, see User Account Control and WMI.

Examples

The following C# sample creates an internal Ethernet port with a dynamic MAC address. The referenced utilities can be found in Common Utilities for the Virtualization Samples.


using System;
using System.Management;

namespace HyperVSamples
{
    class CreateInternalEthernetPortDynamicMacClass
    {
        static ManagementObject CreateInternalEthernetPortDynamicMac(string friendlyName, string name)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization", null);
            ManagementObject switchService = Utility.GetServiceObject(scope, "Msvm_VirtualSwitchManagementService");

            ManagementObject createdInternalEthernetPort = null;

            ManagementBaseObject inParams = switchService.GetMethodParameters("CreateInternalEthernetPortDynamicMac");
            inParams["FriendlyName"] = friendlyName;
            inParams["Name"] = name;
            ManagementBaseObject outParams = switchService.InvokeMethod("CreateInternalEthernetPortDynamicMac", inParams, null);
            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                createdInternalEthernetPort = new ManagementObject(outParams["CreateInternalEthernetPortDynamicMac"].ToString());
                Console.WriteLine("{0} was created successfully", inParams["Name"]);
            }
            else
            {
                Console.WriteLine("Failed to create {0} switch.", inParams["Name"]);
            }
            return createdInternalEthernetPort;
        }

        static void Main(string[] args)
        {
            if (args != null && args.Length != 2)
            {
                Console.WriteLine("Usage: CreateInternalEthernetPortDynamicMac FriendlyName Name");
                Console.WriteLine("Example: CreateInternalEthernetPort \"{0}\" {1}",
                    "First internal Ethernet Port",
                    "FirstInternalEthernetPort");
                return;
            }
            CreateInternalEthernetPortDynamicMac(args[0], args[1]);
        }
    }
}


The following VBScript sample creates an internal Ethernet port with a dynamic MAC address.

dim objWMIService
dim switchService
dim fileSystem

const wmiSuccessful = 0

Main()

'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
    set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
    strComputer = "."

    set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization")
    GetVirtualSwitchManagementServiceInstance
    set createdInternalEthernetPort = CreateInternalEthernetPortDynamicMac
    if Not (createdInternalEthernetPort Is Nothing) then
        WriteLog "Done"
        WScript.Quit(0)
    else
        WriteLog Format1("Err.Number: {0}", Err.Number)
        WriteLog Format1("Err.Description:{0}",Err.Description)
        WScript.Quit(1)
    end if
End Sub

'-----------------------------------------------------------------
' Retrieve GetVirtualSwitchManagementServiceInstance from WMI
'-----------------------------------------------------------------
Sub GetVirtualSwitchManagementServiceInstance()

    query = "select * from Msvm_VirtualSwitchManagementService"
    set managementServiceCol= objWMIService.ExecQuery(query)
    For Each instance in managementServiceCol
        set switchService = instance
    Next

End Sub


'-----------------------------------------------------------------
' Create a virtual switch by calling CreateSwitch WMI method
'-----------------------------------------------------------------
Function CreateInternalEthernetPortDynamicMac()
    set CreateInternalEthernetPortDynamicMac = Nothing
    set objInParam = switchService.Methods_("CreateInternalEthernetPortDynamicMac").InParameters.SpawnInstance_()
    objInParam.FriendlyName = "First Internal Ethernet Dynamic Mac Port"
    objInParam.Name = "FirstInternalDynamicMacPort"

    set objOutParams = switchService.ExecMethod_("CreateInternalEthernetPortDynamicMac", objInParam)

    if objOutParams.ReturnValue = wmiSuccessful then
        set CreateInternalEthernetPortDynamicMac = objWMIService.Get(objOutParams.CreatedInternalEthernetPort)
    else
        WriteLog Format1("CreateInternalEthernetPort failed with error code {0}", objOutParams.ReturnValue)
    end if

End Function

'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
    dim fileStream
    set fileStream = fileSystem.OpenTextFile(".\CreateSwitchPort.log", 8, true)
    WScript.Echo line
    fileStream.WriteLine line
    fileStream.Close

End Sub

'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format1(myString, arg0)
    Format1 = Replace(myString, "{0}", arg0)
End Function

Requirements

Minimum supported client

None supported

Minimum supported server

Windows Server 2008

Namespace

\\.\Root\Virtualization

MOF

WindowsVirtualization.mof

See also

Msvm_VirtualSwitchManagementService

 

 

Send comments about this topic to Microsoft

Build date: 2/29/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ