【问题标题】:Reliably disabling internet access on Windows 7 and up在 Windows 7 及更高版本上可靠地禁用 Internet 访问
【发布时间】:2016-05-10 18:49:59
【问题描述】:

我正在尝试编写一个脚本,该脚本需要完全关闭 Internet,然后再将其重新打开。我希望它能在尽可能多的情况下工作......

  • 支持 Window 7 及更高版本
  • 如果有多个 Internet 连接(如 WiFi 和 LAN)
  • 无论这些连接如何命名
  • 受限用户帐户,UAC?

ipconfig /releaseipconfig /renew,正如 this answer 中所建议的,不适用于 2 个互联网连接。 /release 禁用活动连接(例如 WLAN),但计算机退回到 LAN 连接并且您仍然在线。

$connectedAdapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2"
$connectedAdapters | Invoke-WmiMethod -Name disable

我的问题是:

  • NetConnectionStatus = 2 是一个可靠的互联网访问代理吗?无论您的 NIC 是什么品牌,它都可以在 Windows 7 及更高版本上使用吗?

  • 这是否与启用了 UAC 的受限用户帐户兼容?我想是的……

  • 在我的机器上,此查询还捕获 VirtualBox Host-Only Ethernet Adapter。如果我也禁用/启用它会不会有问题?

  • Get-WmiObjectInvoke-WmiMethod 在 Windows 7 及更高版本上可用吗?

【问题讨论】:

  • @JaquelineVanek Windows 8 及更高版本:(
  • 不确定,但 v5 现在可用于 win7。如果可能的话,alsoq 使用标准帐户可能会很棘手
  • 为什么? “互联网访问”是什么意思?禁用任何连接的适配器也会切断您连接到的任何内部网络,无论是否连接互联网。您究竟想在这里实现什么目标?
  • @MathiasR.Jessen 我知道,这就是我想要的,比如飞行模式。该脚本将作为 boxstarter 脚本的一部分在本地运行。
  • @JaquelineVanek 我知道,但我想分发脚本并支持仍在使用 Windows 7 的人们

标签: windows powershell batch-file command-line nic


【解决方案1】:

问题还没有完全解决,但我开发了一个有效的脚本:

PowerShell Internet Connection helper functions: Go-Offline, Go-Online, Test-InternetAccess

#Requires -Version 2.0

function Test-InternetAccess {
  <#
  .SYNOPSIS
    Tests connectivity by pinging Google DNS servers once
  .DESCRIPTION
    Uses Test-Connection to ping a host, with -Quiet for returning a boolean. The default is a highly available Google DNS server (8.8.4.4)
  .EXAMPLE
    Test-InternetAccess
  .EXAMPLE
    Test-InternetAccess example.com
  .INPUTS
    None.
  .OUTPUTS
    Boolean
  #>
  param (
    [String]
    $RemoteHost = "google-public-dns-b.google.com"
    )
  Test-Connection -Computer $RemoteHost -BufferSize 16 -Count 1 -Quiet
}


function Go-Offline {
  <# 
  .SYNOPSIS
  Disables your internet connection
  .DESCRIPTION
  Finds all network adapters that appear connected and disables them, taking you offline. Later on you can re-enable just those adapters, because they've been stored in an XML file. Connected adapters are detected through WMI. A NetConnectionStatus value of 2 means Connected. 7 means Media Disconnected.
  .EXAMPLE
    Go-Offline
  .INPUTS
    None.
  .OUTPUTS
    None.
  .LINK
    https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/07/use-powershell-to-identify-your-real-network-adapter/
  .LINK
    https://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx
  #>
  [CmdletBinding(SupportsShouldProcess=$True)]
  param()
  $XMLLocation = "$env:TEMP\Disabled-NICs.xml"

  if (Test-InternetAccess) {
    $connectedAdapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2"
    # Go offline
    $connectedAdapters | Invoke-WMIMethod -Name disable 1>$null
    # Save which adapters were connected at the time
    $connectedAdapters | Select Name, DeviceID | Export-Clixml -Path $XMLLocation -Force
    Write-Output "You've been taken offline!"
    Sleep 1
  } else {
    Write-Output "Connection already down..."
    Sleep 1
  }
}

function Go-Online {
  <# 
  .SYNOPSIS
  Re-enables your internet connection
  .DESCRIPTION
  Finds all network adapters that were previously disabled by Go-Offline and enables them. This information is persisted in a temp file.
  .EXAMPLE
    Go-Online
  .INPUTS
    None.
  .OUTPUTS
    None.
  .LINK
    https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/07/use-powershell-to-identify-your-real-network-adapter/
  #>
  [CmdletBinding(SupportsShouldProcess=$True)]
  param()
  $XMLLocation = "$env:TEMP\Disabled-NICs.xml"

  if (!(Test-InternetAccess)) {
    # Get the NICs that have been previously disabled
    $connectedAdapters = Import-Clixml "$env:TEMP\Disabled-NICs.xml" | Select -ExpandProperty DeviceID | ForEach {Get-WMIObject -Class Win32_NetworkAdapter -Filter "DeviceID = $_"}
    # Get back online
    $connectedAdapters | Invoke-WMIMethod -Name enable | Out-Null
    Write-Output "Internet access restored!" # Triggers early, before actual re-connection
    Sleep 1
  }
}

在 Windows 7 上测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-12
    • 2012-10-14
    • 1970-01-01
    • 2017-05-06
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    相关资源
    最近更新 更多