Enable/Disable of the network adapter through MSFT_NetAdapter

3

I'm trying disable/enable the network adapter through MSFT_NetAdapter in OS 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 Disable only. MSFT_NetAdapter returns "DeviceID" or "Name", and when you call method objOperatingSystem.Disable get an error 0x80041003 "Current user does not have permission to perform the action". I try 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 is switched immediately after the code. In OS windows 8 Disable/Enable requires a system reboot after the code. How to manage the network adapter in the OS windows 8 ?

c++
c#-4.0
vbscript
wmi
wmi-query
asked on Stack Overflow May 25, 2015 by Станислав Ра • edited May 25, 2015 by Станислав Ра

1 Answer

3

You need to run with administrator privileges. If your application will be run by users without admin rights, then you could install a service that your application communicates 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)
            {
            }
answered on Stack Overflow Jan 26, 2016 by redlum

User contributions licensed under CC BY-SA 3.0