Scripting VPN Credentials

8k views Asked by At

I can easily create a VPN connection through the PowerShell command Add-VpnConnection, however it doesn't seem able to specify any credentials (there is no option to specify username/password). As a workaround I tried to use -RememberCredential option in Add-VpnConnection and to pass the credentials by forcing a connection through rasdial command, yet even though the connection succeeds Windows doesn't save the credentials :(

Add-VpnConnection -Name xxxxx ...
rasdial xxxxx user password
rasdial xxxxx /disconnect

Is it possible some way ?

1

There are 1 answers

2
user1893999 On

Maybe it is too late, but I had same task and here is solution:

$vpnName = "YourVpnName"
$vpnServerAddress = "YourVpnServerAddress"
$vpnUserName = "YourVpnUserName"
$vpnPassword = "YourVpnPassword"
$vpn = Get-VpnConnection -Name $vpnName
if($vpn -eq $null) {
    Add-VpnConnection -Name $vpnName -ServerAddress $vpnServerAddress -TunnelType Pptp -EncryptionLevel Required -PassThru
    echo "vpn created"
}

if($vpn.ConnectionStatus -eq "Disconnected"){
        $cmd = $env:WINDIR + "\System32\rasdial.exe"
        $expression = "$cmd ""$vpnName"" $vpnUserName $vpnPassword"
        Invoke-Expression -Command $expression 
}