Every so often I have clients requesting that OpenVPN GUI automatically map networked drives once connected to their OpenVPN Server. Fortunately OpenVPN GUI has built in support for running batch scripts once Connected or Disconnected. Note this tutorial is for the Windows based OpenVPN GUI client only. So without further ado, here we go. The process is rather simple, all that is needed are two batch files named the same as the .ovpn
file with either _Up
or _Down
appended to the end. Below you will see the files highlighted in yellow.
It is important to note that these files must be in the same directory as your .ovpn
configuration file. The contents of the files can be seen below. It seems fairly obvious, but note the Home_Up.bat
file will run after connected and the Home_Down.bat
will run after disconnected.
Home_Up.bat
@echo off REM Set the label in the registry reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##172.16.12.10#Admin /v _LabelFromReg /t REG_SZ /f /d "Admin" REM Map the drive net use W: \\172.16.12.10\Admin REM Set the label in the registry reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##172.16.12.10#Downloads /v _LabelFromReg /t REG_SZ /f /d "Downloads" REM Map the drive net use X: \\172.16.12.10\Downloads EXIT
Home_Down.bat
@echo off REM Remove Mapped drives net use W: /delete net use X: /delete EXIT
I’ve tried it and it didn’t work.
There is a timeout and no script running.
Version 2.5.0
Thanks for the update.
thanks, working fine
Hi, could you please share your .ovpn file?
I’ve been trying to make it work but the bat file (Home_up.bat) only works AFTER the connection is made (Initialization Sequence Completed), otherwise I receive a “Network not found” error. Are you using –up option?
Thank you
Jorge,
Unfortunately that is how it works. The .bat file gets called after connected. Just ensure that the .bat file is properly named. It needs to be named the same as your config with the _up.bat at the end. SO for example if I had a “office.ovpn” config file, the batch file would need to be named “office_up.bat”. Hope that helps.
Might as well have ‘Home_Down.bat’ cleanup everything. Leaving those labels behind is a bit sloppy.
@echo off
REM Remove Mapped drives
net use W: /delete
net use X: /delete
REM Cleanup added registry entries
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##172.16.12.10#Admin /f
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##172.16.12.10#Downloads /f
EXIT
Thanks for the ProTip Raj