Enable / disable network adapter via MSFT_NetAdapter

I am trying to disable / enable a network adapter via MSFT_NetAdapter on Windows 8.

strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=Delegate," _
        & "authenticationLevel=pktPrivacy}\root\standardcimv2")

Set colSettings = objWMIService.ExecQuery("Select * from MSFT_NetAdapter")

For Each objOperatingSystem in colSettings 
    Wscript.Echo _ 
    "DeviceID: " & objOperatingSystem.DeviceID & vbCrLf & _
    "Name: " & objOperatingSystem.Name
objOperatingSystem.Disable

Next

      

For example, use only Disable. MSFT_NetAdapter returns "DeviceID" or "Name", and calling objOperatingSystem.Disable gets error 0x80041003 "The current user does not have permission to perform an action." I am trying to use this code:

strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=Delegate," _
        & "authenticationLevel=pktPrivacy}\root\cimv2")

Set colSettings = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where PhysicalAdapter = true")

For Each objOperatingSystem in colSettings 
    Wscript.Echo _ 
    "DeviceID: " & objOperatingSystem.DeviceID & vbCrLf & _
    "Name: " & objOperatingSystem.Name
    objOperatingSystem.Disable
Next

      

This code works fine on windows 7. The network adapter switches right after the code. On Windows 8, Disable / Enable requires system reboot after code. How do I manage the network adapter in Windows 8?

+3


source to share


1 answer


You need to run as administrator. If your app will be run by non-admin users, you can install the service that your app is associated with.

This code disables all network adapters.



            //
            //  In Windows Vista this can be accomplished through a simple WMI query.
            //
            try
            {
                using (var query = new ManagementObjectSearcher("select * from Win32_NetworkAdapter where NetConnectionStatus = 2"))
                {
                    using (var devices = query.Get())
                    {
                        foreach (ManagementObject device in devices)
                        {
                            try
                            {
                                device.InvokeMethod("Disable", null);
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

      

+3


source







All Articles