JuliusPIV
JuliusPIV

Reputation: 155

How to automate VPN connection on Windows 10

I'm going to be traveling for the next month, and I'd like to automate the VPN connection process so that on X event, the script fires and automatically connects me. I've already configured the [L2TP/IPSec] VPN connection in ms-settings:network-vpn & verified it works, but it's automation step that's proving problematic.

Windows GUI: The credentials have been saved.

PowerShell: The RememberCredential property is set to True

VBScript: Curiously, the VPN connection is hidden:

Dim oShell : Set oShell = CreateObject("Shell.Application")
Dim NetConn : Set NetConn = oShell.Namespace(49)
Dim Connections : Set Connections = NetConn.Items
wscript.echo "Connection Count [" & Connections.Count & "]"
For i = 0 to Connections.Count - 1
    wscript.echo "Connections.Item(" & i & ").Name: [" & Connections.Item(i).Name & "]"
next

rasdial <entry>: Expectedly returns error 691.

rasphone -d <entry>: Displays the Connection dialog whereas I'd prefer it to just connect automatically and hidden.

Is this even possible in Windows 10? Or am I just overlooking some small yet key detail?

Upvotes: 0

Views: 9363

Answers (2)

JuliusPIV
JuliusPIV

Reputation: 155

I ended up leveraging Add-VpnConnectionTriggerApplication to trigger an automatic connection of the VPN on the launch of specific executables/UWP applications. The downside is that when doing this, PoSh warns that SplitTunneling must be enabled which is less than ideal.

However after playing around with it for a while (just 2 or so hours now) to ensure the VPN keys off specific executables/UWP's, I ended up disabling SplitTunneling and, paradoxically, it appears to continue working as I would hope/expect. I rebooted a few times, logged on and sure enough by the time the desktop loaded the VPN had been established.

I need to do more testing to confirm, but this is sufficient to help save me from myself.

Upvotes: 2

Kory Gill
Kory Gill

Reputation: 7163

I do this by checking the Remember my sign-in info checkbox when I created the VPN connection.

You can check this in your PowerShell script by ensuring that Get-VpnConnection returns RememberCredential : True.

If this is the case, then rasdial should automatically connect it.

I do it with this:

<#
.SYNOPSIS
Ensures vpn connection (assumed to have saved credentials) is connected.
#>
function Connect-Vpn
{
    [CmdletBinding()]
    param (
        [object]
        $Settings
    )

    $rr1 = Get-VpnConnection -Verbose:$false | where {$_.ServerAddress -imatch $Settings.VpnConnectionPattern -and $_.RememberCredential} | Select -First 1
    if ($rr1.ConnectionStatus -ne 'Connected')
    {
        rasdial.exe $rr1.Name
        If (-not $LASTEXITCODE)
        {
            throw "Cannot connect to '$($rr1.Name)'."
        }
    }
    else
    {
        Write-Verbose "Already connected to '$($rr1.Name)'."
    }
}

You will have to massage this code to your needs as this uses some fields from my settings file...

Upvotes: 0

Related Questions