Last active
August 28, 2024 23:18
-
-
Save ALiangLiang/b66bc6eaeab420a5ff0c2b934c15451e to your computer and use it in GitHub Desktop.
Create and connect VPN with powershell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: ALiangLiang <[email protected]> | |
# Replace VPN_NAME, ADDRESS and ACCOUNT first!! | |
# Ref: https://docs.microsoft.com/en-us/powershell/module/vpnclient/add-vpnconnection?view=win10-ps | |
$VPN_NAME = 'VPN' | |
$ADDRESS = 'vpn.example.com' | |
$ACCOUNT = 'user1' | |
function Connect { | |
# Get password from prompt | |
$pass = Read-Host 'What is your password?' -AsSecureString | |
$pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)) | |
# Establish VPN configuration | |
# Ref: https://docs.microsoft.com/en-us/powershell/module/vpnclient/add-vpnconnection?view=win10-ps | |
Add-VpnConnection -Name "$VPN_NAME" -ServerAddress "$ADDRESS" -TunnelType PPTP -EncryptionLevel Required -RememberCredential -PassThru | |
# Connect the VPN configuration | |
$code = (Start-Process rasdial -NoNewWindow -ArgumentList "$VPN_NAME $ACCOUNT $pass" -PassThru -Wait).ExitCode | |
if ("$code" -eq "0") { | |
Write-Host "Create and connect to VPN server success" -ForegroundColor DarkGreen | |
return $true | |
} else { | |
# Error codes: https://support.microsoft.com/en-us/help/824864/list-of-error-codes-for-dial-up-connections-or-vpn-connections | |
if ("$code" -eq "691") { | |
Write-Host "Create and connect to VPN server failed with wrong username or password" -ForegroundColor DarkRed | |
return $false # return and try againg | |
} else { | |
Write-Host "Create and connect to VPN server failed with error code: $($code)" -ForegroundColor DarkRed | |
throw "$code" | |
} | |
} | |
} | |
function Disconnect { | |
# Disconnect | |
rasdial "$VPN_NAME" /DISCONNECT | |
# Remove it | |
Remove-VpnConnection -Name "$VPN_NAME" -Force | |
Write-Host "Disconnect and remove VPN connection success" -ForegroundColor Magenta | |
} | |
function Pause { | |
param($text) | |
write-host "$text" | |
[void][System.Console]::ReadKey($true) | |
} | |
# If no connection created before... | |
if (-Not [bool] (Get-VpnConnection -Name "$VPN_NAME")){ | |
while (Connect) {} | |
# Pause the progress | |
Pause -text "Press any key to stop VPN..." | |
} | |
Disconnect | |
# Pause the progress | |
Pause -text "Press any key to close window..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment