次の方法で共有


DeleteSwitchPort method of the Msvm_VirtualSwitchManagementService class

Deletes a virtual switch port.

Syntax

uint32 DeleteSwitchPort(
  [in] Msvm_SwitchPort REF SwitchPort
);

Parameters

SwitchPort [in]

Type: Msvm_SwitchPort

A reference to the port to be deleted. See Msvm_SwitchPort.

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 a port from a virtual switch. The referenced utilities can be found in Common Utilities for the Virtualization Samples.

using System;
using System.Management;

namespace HyperVSamples
{
    class DeleteSwitchPortClass
    {
        ManagementObject switchService = null;
        ManagementScope scope = null;

        DeleteSwitchPortClass()
        {
            scope = new ManagementScope(@"root\virtualization", null);
            switchService = Utility.GetServiceObject(scope, "Msvm_VirtualSwitchManagementService");
        }

        ManagementObject GetVirtualSwitch(string switchName)
        {
            string query = string.Format("select * from Msvm_VirtualSwitch where ElementName = '{0}'", switchName);

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query));

            ManagementObjectCollection virtualSwitchs = searcher.Get();

            ManagementObject virtualSwitch = null;

            foreach (ManagementObject instance in virtualSwitchs)
            {
                virtualSwitch = instance;
                break;
            }

            searcher.Dispose();
            return virtualSwitch;
        }


        ManagementObject GetVirtualSwitchPort(ManagementObject virtualSwitch, string switchPortName)
        {
            ManagementObjectCollection switchPorts = virtualSwitch.GetRelated
             (
                 "Msvm_SwitchPort",
                 "Msvm_HostedAccessPoint",
                 null,
                 null,
                 "Dependent",
                 "Antecedent",
                 false,
                 null
             );

            ManagementObject switchPort = null;

            foreach (ManagementObject instance in switchPorts)
            {
                if (instance["ElementName"].ToString() == switchPortName)
                {
                    switchPort = instance;
                    break;
                }
            }

            return switchPort;
        }


        void DeleteSwitchPort(ManagementObject switchPort)
        {
            ManagementBaseObject inParams = switchService.GetMethodParameters("DeleteSwitchPort");

            inParams["SwitchPort"] = switchPort.Path.Path;

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

        static void Main(string[] args)
        {
            if (args != null && args.Length != 2)
            {
                Console.WriteLine("DeleteSwitchPort SwitchFriendlyName SwitchPortFriendlyName");
                Console.WriteLine("Example: DeleteSwitchPort  \"My First Switch\" \"First VirtualSwitch Port\"");
                return;
            }

            string switchName = args[0];
            string switchPortName = args[1];

            DeleteSwitchPortClass deleteSwitch = new DeleteSwitchPortClass();
            ManagementObject virtualSwitch = deleteSwitch.GetVirtualSwitch(switchName);
            ManagementObject virtualSwitchPort = deleteSwitch.GetVirtualSwitchPort(virtualSwitch, switchPortName);
            deleteSwitch.DeleteSwitchPort(virtualSwitchPort);

            virtualSwitch.Dispose();
            virtualSwitchPort.Dispose();
        }
    }
}

The following VBScript sample deletes a port from a virtual switch.

option explicit 

dim objWMIService
dim switchService
dim fileSystem

const wmiSuccessful = 0

Main()

'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
    dim computer, objArgs, friendlyName
    dim switchFriendlyName, switchPortFriendlyName
    dim switch, switchPort

    set objArgs = WScript.Arguments
    if WScript.Arguments.Count = 2 then
       switchFriendlyName = objArgs.Unnamed.Item(0)
       switchPortFriendlyName = objArgs.Unnamed.Item(1)
    else
       WScript.Echo "usage: cscript DeleteSwitchPort SwitchFriendlyName SwitchPortFriendlyName"
       WScript.Echo "Example: DeleteSwitchPort ""My First Switch"" ""First VirtualSwitch Port""" 
       WScript.Quit(1)
    end if     
    
    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 switch = GetVirtualSwitch(switchFriendlyName)
    
    if (switch Is Nothing) then
        WriteLog Format1("Unable to find switch '{0}'.", switchFriendlyName)
        WScript.Quit(1)   
    end if
    
    set switchPort = GetVirtualSwitchPort(switch, switchPortFriendlyName)

    if (switchPort Is Nothing) then
        WriteLog Format1("Unable to find switchPort '{0}'.", switchPortFriendlyName)
        WScript.Quit(1)   
    end if    
    
    if DeleteSwitchPort(switchPort) then
        WriteLog "Done"
        WScript.Quit(0)
    else
        WriteLog "DeleteSwitchPort failed"
        WScript.Quit(1)
    end if

End Sub

'-----------------------------------------------------------------
' Retrieve VirtualSwitch 
'-----------------------------------------------------------------
Function GetVirtualSwitch(friendlyName)
    dim query
    set GetVirtualSwitch = Nothing
    query = Format1("select * from Msvm_VirtualSwitch where ElementName = '{0}'", friendlyName)
    set GetVirtualSwitch= objWMIService.ExecQuery(query).ItemIndex(0)
End Function


'-----------------------------------------------------------------
' Retrieve VirtualSwitchPort
'-----------------------------------------------------------------
Function GetVirtualSwitchPort(switch, switchPortFriendlyName)
    dim query, switchPort, switchPorts
    set GetVirtualSwitchPort = Nothing
    query = Format1("ASSOCIATORS OF {{0}} WHERE resultClass = Msvm_SwitchPort", switch.Path_.Path)
    set switchPorts= objWMIService.ExecQuery(query)
    for each switchPort in switchPorts
        if lcase(switchPort.ElementName) = lcase(switchPortFriendlyName) then
            set GetVirtualSwitchPort = switchPort
        end if
    next
End Function

'-----------------------------------------------------------------
' Disconnect a virtual switch by calling DeleteSwitchPort WMI method
'-----------------------------------------------------------------
Function DeleteSwitchPort(virtualSwitchPort)
    dim objInParam, objOutParams
    
    DeleteSwitchPort = false
    set objInParam = switchService.Methods_("DeleteSwitchPort").InParameters.SpawnInstance_()
    objInParam.SwitchPort = virtualSwitchPort.Path_.Path

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

    if objOutParams.ReturnValue = wmiSuccessful then
        DeleteSwitchPort = true
    else
        WriteLog Format1("DeleteSwitchPort 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(".\DeleteSwitchPort.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