Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows 7及更高版本上可靠地禁用internet访问_Windows_Powershell_Batch File_Command Line_Nic - Fatal编程技术网

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

在Windows 7及更高版本上可靠地禁用internet访问,windows,powershell,batch-file,command-line,nic,Windows,Powershell,Batch File,Command Line,Nic,我试图写一个脚本,需要完全关闭互联网,然后再打开它。我希望它能在尽可能多的情况下工作 支持windows7及以上版本 如果打开了多个Internet连接(如WiFi和LAN) 不管这些连接是如何命名的 有限用户帐户,UAC ipconfig/release和ipconfig/renew,如中所建议,不能与2个internet连接一起使用/release禁用活动连接(如WLAN),但计算机会返回LAN连接,而您仍然在线 $connectedAdapters = Get-WmiObject -C

我试图写一个脚本,需要完全关闭互联网,然后再打开它。我希望它能在尽可能多的情况下工作

  • 支持windows7及以上版本
  • 如果打开了多个Internet连接(如WiFi和LAN)
  • 不管这些连接是如何命名的
  • 有限用户帐户,UAC
ipconfig/release
ipconfig/renew
,如中所建议,不能与2个internet连接一起使用
/release
禁用活动连接(如WLAN),但计算机会返回LAN连接,而您仍然在线

$connectedAdapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2"
$connectedAdapters | Invoke-WmiMethod -Name disable
我的问题是:

  • NetConnectionStatus=2
    是可靠的internet访问代理吗?无论NIC的品牌如何,它是否在Windows 7及更高版本上可用

  • 这是否与UAC上的有限用户帐户兼容?我想是的

  • 在我的机器上,此查询还捕获VirtualBox纯主机以太网适配器。如果我也禁用/启用它会有问题吗

  • Windows 7及更高版本是否提供了
    Get WmiObject
    Invoke WmiMethod


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

#需要-版本2.0
功能测试InternetAccess{
param(
[字符串]
$RemoteHost=“google-public-dns-b.google.com”
)
测试连接-计算机$RemoteHost-缓冲区大小16-计数1-安静
}
功能离线{

@JaquelineVanek Windows 8及更高版本:(不确定,但v5现在可用于win7。如果可能的话,使用标准帐户可能会很棘手。为什么?什么是“internet访问”意思是?禁用任何已连接的适配器也会切断您与任何已连接的内部网络的连接,无论是否已连接internet。您到底想在这里实现什么?@MathiasR.Jessen我知道,这就是我想要的,就像飞机模式。该脚本将在本地运行,作为boxstarter脚本的一部分。@JaquelineVanek我知道,但我想要分发脚本并支持仍在Windows 7上的用户
#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
  }
}