Partager via


DeleteInternalEthernetPort method of the Msvm_VirtualSwitchManagementService class

Deletes an internal Ethernet port.

Syntax

uint32 DeleteInternalEthernetPort(
  [in] Msvm_InternalEthernetPort REF InternalEthernetPort
);

Parameters

InternalEthernetPort [in]

Type: Msvm_InternalEthernetPort

A reference to the internal Ethernet port to be deleted. See Msvm_InternalEthernetPort.

Return value

Type: uint32

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

Completed with No Error (0)

Method Parameters Checked - Job Started (4096)

Failed (32768)

Access Denied (32769)

Not Supported (32770)

Status is unknown (32771)

Timeout (32772)

Invalid parameter (32773)

System is in used (32774)

Invalid state for this operation (32775)

Incorrect data type (32776)

System is not available (32777)

Out of memory (32778)

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 deletes an internal Ethernet port. The referenced utilities can be found in Common Utilities for the Virtualization Samples.

using System;
using System.Management;

namespace HyperVSamples
{
    class DeleteInternalEthernetPortClass
    {
        static void DeleteInternalEthernetPort(string internalPortName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization", null);
            ManagementObject switchService = Utility.GetServiceObject(scope, "Msvm_VirtualSwitchManagementService");

            ManagementBaseObject inParams = switchService.GetMethodParameters("DeleteInternalEthernetPort");

            ManagementObject ethernetPort = Utility.GetHostSystemDevice("Msvm_InternalEthernetPort", internalPortName, scope);
            inParams["InternalEthernetPort"] = ethernetPort.Path.Path;

            ManagementBaseObject outParams = switchService.InvokeMethod("DeleteInternalEthernetPort", inParams, null);
            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                Console.WriteLine("{0} was deleted successfully", ethernetPort.Path.Path);
            }
            else
            {
                Console.WriteLine("Failed to delete {0} switch.", ethernetPort.Path.Path);
            }
        }

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

The following VBScript sample deletes an internal Ethernet port.

option explicit 

dim objWMIService
dim switchService
dim fileSystem

const wmiSuccessful = 0

Main()

'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()

    dim computer, internalEthernetPort, friendlyName, objArgs
    set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
    computer = "."

    set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization")
    set switchService = objWMIService.ExecQuery("select * from Msvm_VirtualSwitchManagementService").ItemIndex(0)
    
    set objArgs = WScript.Arguments
    if WScript.Arguments.Count = 1 then
       friendlyName = objArgs.Unnamed.Item(0)
    else
       WScript.Echo "usage: cscript DeleteInternalEthernetPort FriendlyName"
       WScript.Echo "Example: DeleteInternalEthernetPort ""First Internal Ethernet Port""" 
       WScript.Quit(1)
    end if    
    
    set internalEthernetPort = GetInternalEthernetPort(friendlyName)
    if Not (createdSwitch Is Nothing) then
        if DeleteInternalEthernetPort(internalEthernetPort) then
            WriteLog "Done"
            WScript.Quit(0)
        end if
    else
        WriteLog "DeleteInternalEthernetPort Failed"
        WScript.Quit(1)
    end if
End Sub

'-----------------------------------------------------------------
' Retrieve internalEthernetPort wmi object
'-----------------------------------------------------------------
Function GetInternalEthernetPort(friendlyName)
    dim query
    set GetInternalEthernetPort = Nothing
    query = Format1("select * from Msvm_InternalEthernetPort where ElementName = '{0}'", friendlyName)
    set GetInternalEthernetPort= objWMIService.ExecQuery(query).ItemIndex(0)
End Function

'-----------------------------------------------------------------
' Delete internal Ethernet port by calling DeleteInternalEthernetPort
'-----------------------------------------------------------------
Function DeleteInternalEthernetPort(internalEthernetPort)
    dim objInParam, objOutParams
    DeleteInternalEthernetPort = false
    set objInParam = switchService.Methods_("DeleteInternalEthernetPort").InParameters.SpawnInstance_()
    objInParam.InternalEthernetPort = internalEthernetPort.Path_.Path

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

    if objOutParams.ReturnValue = wmiSuccessful then
        DeleteInternalEthernetPort = true
    else
        WriteLog Format1("DeleteInternalEthernetPort 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(".\DeleteInternalEthernetPort.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
End of client support
None supported
End of server support
Windows Server 2012
Namespace
Root\Virtualization
MOF
WindowsVirtualization.mof

See also

Msvm_VirtualSwitchManagementService