在 Windows 7 及更高版本上可靠地禁用互联网访问

Reliably disabling internet access on Windows 7 and up

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

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

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

我的问题是:

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

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.