通过MSFT_NetAdapter

问题描述:

启用网络适配器/禁用

我试图禁用/启用通过MSFT_NetAdapter网络适配器操作系统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精细的网络适配器的代码之后立即切换。在操作系统的Windows 8禁用/启用代码后需要重启系统。 如何管理OS 8中的网络适配器?

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

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

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