如何检测具有特定名称的活动 VPN 连接

lex*_*eme 5 powershell vpn powershell-2.0 windows-server-2008-r2

我发现一篇文章列出了检测活动 VPN 的几种方法:

  1. 使用 Win32_NetworkAdapter:

    Get-WmiObject -Query "Select * from Win32_NetworkAdapter 
    where Name like '%VPN%' and NetEnabled='True'"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用 Win32_NetworkAdapterConfiguration:

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

    ipconfig | Select-String 'PPP adapter'
    
    Run Code Online (Sandbox Code Playgroud)
  4. 检查 IPv4 路由表

    Get-WmiObject -Query "Select * from Win32_IP4RouteTable 
    where Name like '10.0.99.%' or Name like '10.15.99.%'"
    
    Run Code Online (Sandbox Code Playgroud)

就我而言,方法 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
    }
    
    Run Code Online (Sandbox Code Playgroud)

    为我提供了适配器名称列表,其描述中没有提及“VPN”:

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

小智 6

最简单的方法是使用 Powershell Commamdlet

Get-VPNconnection -Name "VPN Name"
Run Code Online (Sandbox Code Playgroud)

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

  • 这似乎并不适用于所有 VPN。我刚刚尝试连接到 Fortinet VPN (FortiClient),但没有从 Get-VPNconnection 获得任何结果 (3认同)