VPN and RDP: Automating remote workplace configuration
Deploy VPN and RDP clients for remote users
As part of this blog, there were publications about setting up a VPN server.
2020 brought everyone a surprise in the form of a eaten bat.
In March of this year, I had to organize several dozen remote jobs using standard tools (
Windows 7, 8, and 10).
For myself, I chose a bundle of Softether VPN as a VPN server and Windows Terminal Server as a Terminal Server

Simple automation scripts were created for fast deployment on the client side of VPN and RDP.
Looking ahead — I didn't manage to fully automate the setup process.I managed to reduce the setup time by 70-80%.
General idea:
- Automate VPN and RDP connections by running a single file.
- When RDP sessions are disabled, they will be disconnected from the VPN server.
- Speed up client-side deployment
Realization:
- VPN Connection Template
- RDP File Template
- Cmdow program to hide the active CMD window
- Bat connection Automation script
- Bat script for creating client connections
We assume that the VPN server (L2TP/IPsec) is configured and available at
vpn.mydomain.ru.
The network still has descriptions about creating terminal servers with port forwarding to the Internet.
As protection, they suggest changing the standard port 3389 to another one.
!Never do this, be sure to put a VPN server in the gap!
Creating a project directory:
Code:
mkdir %SystemDrive%\VPNconnect
Creating a "template" for a VPN connection:
Setting up and testing a VPN connection, after a successful test, we save its data to a text file called my. pbk.
To do this, use a text editor to open the file:
Code:
notepad %USERPROFILE%\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk
If you have only one connection on your machine, you can simply copy this file to the project's working folder.
If there are multiple connections, then use your hands to copy the data of the desired connection to a new file.
Landmark — connection name in square brackets:
[vpn.mydomain.ru]
End of configuration ends with a block:
DEVICE=vpn
Full text of the configuration
We will import this file to our clients in the future.
When importing, we will lose your username, password, and shared IPsec key.
Create an RDP file in the project directory (a text file with the rdp extension):
RDP File.
We pay attention to 3 lines and change them to the necessary ones:
- full address: s: 192.168.66.223 — the address of your terminal server
- drivestoredirect:s:Local disk (C
— - redirects the user's local disk
- username: s:MYDOMAIN\vladimir — username of the user
More complete description of available variables in the rdp file
Adding it to the project directory cmdow.exe from
the downloaded archive at the path \bin\Release (
alternative link).
In total, we have 3 files in the directory:
- my. pbk-VPN connection template
- my. rdp-RDP file
- cmdow.exe -a program for hiding the script's working window
Creating 2 bat scripts in the project directory:
- install.bat-script installer
- connect.bat - script for launching VPN and RDP sessions
Code:
notepad++ %SystemDrive%\VPNconnect\connect.bat
cmdow @ /HID
rasdial vpn.mydomain.ru username password
mstsc.exe my.rdp
rasdial vpn.mydomain.ru /disconnect
notepad++ %SystemDrive%\VPNconnect\install.bat
copy my.rdp connect.bat %USERPROFILE%\Desktop
copy cmdow.exe %LOCALAPPDATA%|
type my. pbk >>> %USERPROFILE% \ AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone. pbk
REM Open network adapters in the control panel to install the IPsec shared key
ncpa. cpl
Fill in the correct RDP file data (server address, user name).
In
the connect file.bat enter the correct username and password.
Deploying clients
Copy the project directory to the client's computer.
Go to the directory and run
install. bat as an administrator.
Setting
the IPsec key in the network adapter properties.
Two files appeared on the client's desktop:
- connect.bat-raises the VPN connection, RDP session (based on the my.rdp file)
extinguishes the VPN connection after disabling the RDP session
- my. rdp — required for the RDP session
After starting
connect.bat will connect to the VPN server and then start the RDP session.
That's probably all
P.S. From the mass of various home computers came across two copies:
Windows XP — in general, neither the standard tools could not configure the VPN
Windows 7 (32) — when trying to connect, it returned various errors
I had to install a VPN client from SoftEther on these two machines.
Once it is configured, you can automate its launch using its CLI:
- vpncmd localhost /client /cmd AccountConnect Connect_name
- vpncmd localhost /client /cmd AccountDisconnect Connect_name
Rating
More info: