【问题标题】:How to detect an active VPN connection with a specific name如何检测具有特定名称的活动 VPN 连接
【发布时间】:2017-12-25 16:33:23
【问题描述】:

我找到了一个article,其中列出了几种检测活动 VPN 的方法:

  1. 使用 Win32_NetworkAdapter:

    Get-WmiObject -Query "Select * from Win32_NetworkAdapter 
    where Name like '%VPN%' and NetEnabled='True'"
    
  2. 使用 Win32_NetworkAdapterConfiguration:

    Get-WmiObject -Query "Select * from Win32_NetworkAdapterConfiguration 
    where Description like '%VPN%' and IPEnabled='True'"
    
  3. 检查是否存在活动的 PPP 适配器

    ipconfig | Select-String 'PPP adapter'
    
  4. 检查 IPv4 路由表

    Get-WmiObject -Query "Select * from Win32_IP4RouteTable 
    where Name like '10.0.99.%' or Name like '10.15.99.%'"
    

在我的情况下,方法 1,2 和 4 根本不起作用。第三种方法效果很好,真的足够了。但据我了解,配置多个 VPN 连接会给方法 3 带来模棱两可的结果。

所以我的问题是:

  1. 有没有更具体的方法(可能使用组合方法?)方法 3,以便能够检查连接名称?
  2. 为什么方法 1 和 2 对我不起作用?像这样查询它

    $adapters = (Get-WmiObject -Query "Select * from Win32_NetworkAdapter")
    foreach($a in $adapters) {
        Write-Host $a.Description
    }
    

    给我一​​个适配器名称列表,在描述中没有提到“VPN”:

    WAN 微型端口 (SSTP) WAN 微型端口 (IKEv2) WAN 微型端口 (L2TP) WAN 微型端口 (PPTP) 等等...

【问题讨论】:

  • 好吧,您能否在具有 2 个活动 VPN 连接的 PC 上检查 ipconfig 的输出,以辨别它们是否以某种方式存在差异并据此进行过滤?在最坏的情况下你会得到ppp adapter 2 次,但我相信它们会有所不同,你可以找到一种过滤东西的方法

标签: powershell powershell-2.0 vpn windows-server-2008-r2


【解决方案1】:

最简单的方法是使用 Powershell Commamdlet

Get-VPNconnection -Name "VPN Name"

报告的项目之一是“ConnectionStatus”。此外,如果 VPN 是所有用户连接,您还必须使用带有命令行开关的开关“-AllUserConnection”

【讨论】:

  • 这似乎不适用于所有 VPN。我刚刚尝试连接到 Fortinet VPN (FortiClient) 并没有从 Get-VPNconnection 得到任何结果
【解决方案2】:

用这个怎么样:

Get-NetAdapter -InterfaceDescription "VPN Name" | where {$_.status -eq "Up"}

只需使用Get-NetAdapter 获取网络/VPN 名称列表..

【讨论】:

    【解决方案3】:

    如果您还想获取 PPP 适配器和其他隐藏的适配器信息,您应该使用这个:

    [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
    

    【讨论】:

      【解决方案4】:

      在我的场景中,InterfaceDescription 属性中 TAP 的存在表示 VPN 连接状态,但您可以在测试中放置任何字符串

      $NIC = Get-NetIPConfiguration | ? {$_.NetAdapter.status -eq "UP"} | Select-Object InterfaceDescription
          if ($NIC -match "TAP") {
      #VPN active → go to work
      } else { 
             Write-Host "~~~~~~~~~~~~~~~~~" -ForegroundColor Cyan 
             Write-Host "VPN DEACTIVATED ?" -ForegroundColor Cyan 
             Write-Host "~~~~~~~~~~~~~~~~~" -ForegroundColor Cyan 
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-23
        • 1970-01-01
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        • 2011-03-12
        • 2015-07-15
        • 1970-01-01
        相关资源
        最近更新 更多