I am a newb to Powershell scripting so I can take no credit for the code here as it has all been borrowed from what I have found online.
I would like to use a powershell script to add a route bound to a L2TP VPN connection.
First I make my L2TP connection.
Then I use (Get-WmiObject -Class Win32_NetworkAdapterConfiguration) to find the index of the L2TP VPN.
(partial output)
DHCPEnabled : False
IPAddress : {10.10.1.5}
DefaultIPGateway : {10.10.1.5}
DNSDomain :
ServiceName : NdisWan
Description : WAN (PPP/SLIP) Interface
Index : 589827
Then:
$ping = New-Object System.Net.NetworkInformation.Ping
$ips = $($ping.Send(VPN Provider.com).Address).IPAddressToString
route add $ips mask 255.255.255.255 192.168.1.1 if 589827
This fails:
route.exe : The route addition failed: Either the interface index is wrong or the gateway does not lie on the same network as the interface. Check the IP Address Table for the machine.
When I run Route print I see in the Interface List:
0x130002 ...00 53 45 00 00 00 ...... WAN (PPP/SLIP) Interface
This Works:
route add $ips mask 255.255.255.255 192.168.1.1 if 0x130002
But the problem of using 0x130002 is that it changes each time the L2TP VPN is connected.
So, how do I get the dynamic WAN (PPP/SLIP) Interface number in a powesrhell variable?