I found an article which lists several ways to detect an active VPN:
Using Win32_NetworkAdapter:
Get-WmiObject -Query "Select * from Win32_NetworkAdapter where Name like '%VPN%' and NetEnabled='True'"
Using Win32_NetworkAdapterConfiguration:
Get-WmiObject -Query "Select * from Win32_NetworkAdapterConfiguration where Description like '%VPN%' and IPEnabled='True'"
Checking the presence of an active PPP adapter
ipconfig | Select-String 'PPP adapter'
Checking the IPv4 routing table
Get-WmiObject -Query "Select * from Win32_IP4RouteTable where Name like '10.0.99.%' or Name like '10.15.99.%'"
In my case approaches 1,2 & 4 simply didn't work. The third approach works well and it's really enough. But as far as I understand having multiple VPN connections configured can give an ambiguous result for method 3.
So my questions are:
- Is there a way to be more specific (use combined approach maybe?) with method 3, to be able to check the connection name?
Why didn't methods 1 & 2 work for me? Querying it like this
$adapters = (Get-WmiObject -Query "Select * from Win32_NetworkAdapter") foreach($a in $adapters) { Write-Host $a.Description }
gives me a list of adapter names with no mention of 'VPN' in their description:
WAN Miniport (SSTP) WAN Miniport (IKEv2) WAN Miniport (L2TP) WAN Miniport (PPTP) and so on...
ipconfig
on a PC with 2 active VPN connections to discern if they differ somehow and filter based on that? at the very worst you will getppp adapter
2 times, but I'm sure they will differ and you can work out a way to filter stuff – Oquendo