Export (0) Print
Expand All

DisconnectSwitchPort method of the Msvm_VirtualSwitchManagementService class

Hyper-V

Disconnects a virtual switch port.

Syntax


uint32 DisconnectSwitchPort(
  [in]  Msvm_SwitchPort REF SwitchPort
);

Parameters

SwitchPort [in]

Type: Msvm_SwitchPort

The switch port to be disconnected. See Msvm_SwitchPort.

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


using System;
using System.Management;

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

        DisconnectSwitchPortClass()
        {
            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().ToLower() == switchPortName.ToLower())
                {
                    switchPort = instance;
                    break;
                }
            }

            return switchPort;
        }

        void DisconnectSwitchPort(ManagementObject switchPort)
        {

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

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

        void Close()
        {
            this.switchService.Dispose();
        }

        static void Main(string[] args)
        {
            if (args != null && args.Length != 2)
            {
                Console.WriteLine("DisconnectSwitchPort switchFriendlyName switchPortFriendlyName");
                Console.WriteLine("Example: DisconnectSwitchPort MyFirstSwitch \"My Switch Port\"");
                return;
            }

            DisconnectSwitchPortClass disconnectSwitchPort = new DisconnectSwitchPortClass();
            ManagementObject virtualSwitch = disconnectSwitchPort.GetVirtualSwitch(args[0]);
            ManagementObject virtualSwitchPort = disconnectSwitchPort.GetVirtualSwitchPort(virtualSwitch, args[1]);
            disconnectSwitchPort.DisconnectSwitchPort(virtualSwitchPort);
            virtualSwitch.Dispose();
            virtualSwitchPort.Dispose();
            disconnectSwitchPort.Close();
        }
    }
}


The following VBScript sample disconnects a switch port.


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 DisconnectSwitchPort SwitchFriendlyName SwitchPortFriendlyName"
       WScript.Echo "Example: DisconnectSwitchPort ""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 DisconnectSwitchPort(switchPort) then
        WriteLog "Done"
        WScript.Quit(0)
    else
        WriteLog "DisconnectSwitchPort 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 DisconnectSwitchPort WMI method
'-----------------------------------------------------------------
Function DisconnectSwitchPort(virtualSwitchPort)
    dim objInParam, objOutParams
    
    DisconnectSwitchPort = false
    set objInParam = switchService.Methods_("DisconnectSwitchPort").InParameters.SpawnInstance_()
    objInParam.SwitchPort = virtualSwitchPort.Path_.Path

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

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

 

 

Community Additions

ADD
Show:
© 2014 Microsoft