【问题标题】:Enable/Disable of the network adapter through MSFT_NetAdapter通过 MSFT_NetAdapter 启用/禁用网络适配器
【发布时间】:2015-05-25 07:48:31
【问题描述】:

我正在尝试通过 OS Windows 8 中的 MSFT_NetAdapter 禁用/启用网络适配器。

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

例如,仅使用禁用。 MSFT_NetAdapter 返回“DeviceID”或“Name”,当您调用方法 objOperatingSystem.Disable 时会收到错误 0x80041003“当前用户无权执行该操作”。 我尝试使用此代码:

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

此代码在 Windows 7 上运行良好。在代码之后立即切换网络适配器。在 OS windows 8 中,禁用/启用需要在代码后重新启动系统。 如何在 OS windows 8 中管理网络适配器?

【问题讨论】:

    标签: c++ c#-4.0 vbscript wmi wmi-query


    【解决方案1】:

    您需要以管理员权限运行。如果您的应用程序将由没有管理员权限的用户运行,那么您可以安装与您的应用程序通信的服务。

    此代码禁用所有网络适配器。

                //
                //  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)
                {
                }
    

    【讨论】:

      猜你喜欢
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多