次の方法で共有


DeleteSwitch method of the Msvm_VirtualSwitchManagementService class

Deletes a virtual switch.

Syntax

uint32 DeleteSwitch(
  [in] Msvm_VirtualSwitch REF VirtualSwitch
);

Parameters

VirtualSwitch [in]

Type: Msvm_VirtualSwitch

A reference to the switch to be deleted. See Msvm_VirtualSwitch.

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

using System;
using System.Management;

namespace HyperVSamples
{
    class DeleteSwitchClass
    {
        static ManagementObject GetVirtualSwitch(string switchName, ManagementScope scope)
        {
            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;
        }

        static void DeleteSwitch(string switchFriendlyName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization", null);
            ManagementObject switchService = Utility.GetServiceObject(scope, "Msvm_VirtualSwitchManagementService");

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

            ManagementObject virtualSwitch = GetVirtualSwitch(switchFriendlyName, scope); 
            inParams["VirtualSwitch"] = virtualSwitch.Path.Path;
            ManagementBaseObject outParams = switchService.InvokeMethod("DeleteSwitch", inParams, null);
            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                Console.WriteLine("{0} was deleted successfully", virtualSwitch["Name"]);
            }
            else
            {
                Console.WriteLine("Failed to delete {0} virtual switch. Error {1}.", virtualSwitch["Name"], outParams["ReturnValue"]);
            }
        }

        static void Main(string[] args)
        {
            if (args != null && args.Length != 3)
            {
                Console.WriteLine("Usage: DeleteSwitch FriendlyName");
                Console.WriteLine("Example: DeleteSwitch \"My First Switch\"");
                return;
            }
            DeleteSwitch(args[0]);
        }
    }
}

The following VBScript sample deletes a virtual switch.

option explicit 

dim objWMIService
dim switchService
dim fileSystem

const wmiSuccessful = 0

Main()

'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
    dim computer, objArgs, friendlyName, createdSwitch

    set objArgs = WScript.Arguments
    if WScript.Arguments.Count = 1 then
       friendlyName = objArgs.Unnamed.Item(0)
    else
       WScript.Echo "usage: cscript DeleteSwitch FriendlyName"
       WScript.Echo "Example: DeleteSwitch ""My First Switch""" 
       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 createdSwitch = GetVirtualSwitch(friendlyName)
    if Not (createdSwitch Is Nothing) then
        if DeleteSwitch(createdSwitch) then
            WriteLog "Done"
            WScript.Quit(0)
        End if
    else
        WriteLog "DeleteSwitch 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

'-----------------------------------------------------------------
' Delete a virtual switch by calling DeleteSwitch WMI method
'-----------------------------------------------------------------
Function DeleteSwitch(virtualSwitch)
    dim objInParam, objOutParams 
    DeleteSwitch = false
    set objInParam = switchService.Methods_("DeleteSwitch").InParameters.SpawnInstance_()
    objInParam.VirtualSwitch = virtualSwitch.Path_.Path

    set objOutParams = switchService.ExecMethod_("DeleteSwitch", objInParam)
    
    if objOutParams.ReturnValue = wmiSuccessful then
        DeleteSwitch = true
    else
        WriteLog Format1("Delete 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(".\DeleteSwitch.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