I started blogging once after upgrading to Windows 7 & posted mostly about the ridiculous bugs Microsoft exported with that OS. Gradually Windows 7 got matured (hardly ever fixing the yellow triangle network icon issue) & my entire attention switched to what I do for salaries, Oracle development & later much of my posts were about the stack.
Now Microsoft has released another half cooked OS, Windows 11. From a layman perspectives I cannot understand how someone could make such decisions that affect the established stability and ease of use of an OS that creates huge disappointments for general userbase!
Other than cosmetic changes and revamped settings area, I cannot defer Windows 11 from Windows 10, plus the disappointment of losing the start menu that I was getting used to after loads of patience and efforts. Among many of such grievances, connecting to VPN that is defined using Windows new connection wizard at work is so ridiculous, requiring 4 mouse clicks! So I decided to go with a cmd/powershell script this time to avoid those 4 mouse clicks. If you are using Cisco or other VPN solutions, this will not apply to you.
After weighing the possibilities of extending , I decided to go with PowerShell (Version 5)
So let us check how it works. Copy the following in to a text file
rasdial.exe "Your VPN Name"
Save the file as “Dial VPN.ps1” or any other name you prefer with extension “.ps1”.
Make sure you wrap the VPN name using double quotes. “rasdial.exe” is not a powershell cmdlet, an old Windows OS friendly dialer. This executable is generally found in the Windows\System32 folder and there is no need to specify the path, unless you modified the PATH environment variable. Now create a new shortcut on the desktop and type/copy the following (Please adjust the file path as per your setup) as command for the shortcut
powershell.exe -File "C:\scripts\Dial-VPN.ps1"
Please note, you must configure the Powershell execution police “RemoteSigned” inorder to make this work. Example:
PS C:\Users\<username>> Set-ExecutionPolicy RemoteSigned
Usually I keep all my scripts in a folder named “Scripts” on the C: drive. Hence the -File parameter clearly mentions the script’s path.
That’s all, you can double click and open the pre-configured VPN connection without going through the 4 click hassles on Windows 11 (or multiple clicks on other Windows OS versions)
Now, let us look at a fancier version of the above. What if you want to connect if not connected and disconnect if already connected? Please note, this script could be extended beyond this level depending upon specific requirements. I have started with the skeleton and will be happy to receive better scripts from you.
Code block modified on 11th December 2022 for better error handling.
<#
https://community.spiceworks.com/topic/2271983-auto-connect-vpn
http://woshub.com/popup-notification-powershell/
POWERSHELL error handling for Invoke-Expression
https://stackoverflow.com/questions/32348794/how-to-get-status-of-invoke-expression-successful-or-failed
#>
$vpnname = 'Your VPN Name'
$vpnusername = "YOURUSERNAME"
$vpnpassword = "YOURPASSWORD"
$cmd = $env:WINDIR + "\System32\rasdial.exe"
try {
$vpn = Get-VpnConnection -Name $vpnname -ErrorAction Stop
$Success = $vpn.ConnectionStatus
}
catch {
Write-Host "Please use Windows Network and Internet Setting to define a VPN Connection"
}
try {
if ($vpn.ConnectionStatus -eq "Disconnected") {
$expression = "$cmd ""$vpnname"" "
$Success = Invoke-Expression -Command $expression
if ($LASTEXITCODE -ne 0) {
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("$Success", 0, "$vpnname Status", 64)
}
else {
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("Connected.", 0, "$vpnname Status", 64)
}
}
else {
$vpnstatus = $vpn.ConnectionStatus
if ($vpnstatus -eq "Connected") {
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("$vpnname will be disconnected, Are you sure?", 0, "$vpnname Status", 4 + 32)
if ($Output -eq 6) {
$expression = "$cmd ""$vpnname"" /DISCONNECT"
Write-Host $expression
Invoke-Expression -Command $expression
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("VPN Disconnected", 0, "$vpnname Status", 64)
}
}
}
}
catch {
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("There was an unexpected error connecting VPN," +
" Please check whether you have defined the $vpnname correctly." +
" Check your username & password, Server IP address etc", 0, "$vpnname Status", 64)
}
Please note, I am a beginner with PowerShell(Also) and always will be. As usual, I have provided the links to original codes & possible other links those helped me to device the above.
The above can, dial your VPN connection, warn you before you disconnect etcetera. For my ease, I prefer to use the extended version of the script as I keep on switching the connections. You may able to extend the script once again by accepting the VPN connection name, so that you can use the same script for dialing different VPN connections (if you have many)
Have suggestions? please pass them to me through the comments section.