Open a VPN before connect
Some servers can only be reached through a VPN. Then you have to open the tunnel first, wait until it is up, and only then start the remote session. 1Remote can do the waiting for you: open the VPN in Script before connect and close it in Script after disconnected.
The example uses OpenVPN (its program is called openvpn-gui). The idea works with any VPN that can be started from a command line.

The script before connect: open the VPN and wait
open_vpn.ps1:
# SERVER_HOST looks like "192.168.50.10:3389"
$address, $port = $env:SERVER_HOST.Split(':')
# tells the running openvpn-gui to connect the config "myconnection.ovpn"
& "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --command connect myconnection.ovpn
function Test-Port($hostname, $port, $timeoutMs = 2000) {
$client = New-Object System.Net.Sockets.TcpClient
try {
$result = $client.BeginConnect($hostname, $port, $null, $null)
if ($result.AsyncWaitHandle.WaitOne($timeoutMs)) { return $client.Connected }
return $false
} catch { return $false } finally { $client.Close() }
}
Write-Host "Waiting for the VPN..." -NoNewLine
$i = 0
while (-not (Test-Port $address $port)) {
if ($i++ -ge 60) {
Write-Error "The server $address is still not reachable after 60 tries."
exit 1 # any code other than 0 stops the connection
}
Write-Host -NoNewLine ' .'
Start-Sleep -Seconds 1
}
Write-Host ""
Write-Host "The VPN is up, connecting to $address"
exit 0
What it does:
- It reads the address and the port of the server from
SERVER_HOST, one of the variables every script gets. So the same script works for every server. - It asks the running openvpn-gui to connect
myconnection.ovpn. Replace the name with your config file; the.ovpnfile is enough, openvpn-gui finds it in its config folder. The command only talks to openvpn-gui, so openvpn-gui has to run (it starts automatically with Windows when OpenVPN was installed with that option). - It tries to reach the server port up to 60 times, each try waits up to 2 seconds and is followed by a pause of 1 second, so it gives up after about 3 minutes at most. As soon as the server answers, the script ends with exit code
0and 1Remote connects. After 60 tries it ends with exit code1, and 1Remote showsScript ExitCode = 1, connection abort!instead of connecting.
The script after disconnected: close the VPN
close_vpn.ps1:
& "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --command disconnect myconnection.ovpn
Set it up
- Save both scripts, for example in
D:\scripts\. - Open the server with
Edit, pickopen_vpn.ps1asScript before connectandclose_vpn.ps1asScript after disconnected(clickSelect). - Click
Testnext to the first script: 1Remote tells you what it will run, runs the script (its window shows a dot for every try) and then showsThe exit code of the script = 0when the server answered. - Leave
Hide script windowunticked for this script: it waits for the VPN, and with a hidden window you can not see whether the VPN is still coming up or the script hangs. - Click
Save.
Note
If other people use the same server from the shared database, the scripts run for them too, and fail when they do not have D:\scripts\open_vpn.ps1. Put the scripts on all those PCs, or keep the server in a database only you use.
✏️ Last Change: 2026-11-24 01:17
📖 Publication Date: 2026-11-24 01:27