Techblog Powershell Script to connect VPN when outside Office network (waits for internet connection first)

 In Uncategorized

ConnectVPN powershell script was created for Office Laptops to automatically connect a VPN when a laptop is outside the Office. The script works with any standard Win 10 VPN connection.

Laptops outside the office need some time to get internet connected after login. The user has to connect to wifi after login before the script connects the vpn.
This is why the scrip first waits untill the computer is connected to the internet.
Next to determine if its on the Office LAN or outside the office it checks if a unique host on the LAN is reachable if not it assumes its outside the office LAN and dials the VPN. The script keeps checking if the VPN is connected if not it reconnects the VPN.

To install and use place all files in the same folder
Edit ConnectVPN.ps1 using Notepad and fillin the parameters
$InternetHost this host is used to determine if the computer is connected to the internet.
You can leave the value at 8.8.8.8 (google DNS)
$LanHost this host is used to determine if the computer is connected to the LAN office network and if it is
the VPN is not dialed. Choose the name of a unique pingable host on you Office LAN network. For example the full server name.
$VPNName name of the VPN connection.
$VPNUsername fillin the VPN username (leave it blank when the windows login account is used for the VPN
for example when using SSTP VPN)
$VPNUsername fillin the VPN password (leave it blank when the windows login account is used for the VPN
for example when using SSTP VPN)

Run SetupConnectVPN as an administrator
SetupConnectVPN creates a task in the task scheduler which runs ConnectVPN.bat at logon.
The script runs in a minimized dos box and keeps running until its stopped.

You can also run ConnectVPN.bat manually to connect the VPN or create a shortcut to connectVPN.bat and
put the shortcut in the C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp folder

Click here to download the script including all setup files

Recent Posts