Scripts before connect and after disconnect
For every server you can set two scripts: one that runs before 1Remote connects, and one that runs after the session was disconnected. A script is a .bat or .cmd file, a .ps1 PowerShell script or a .py Python script. The usual case is to open a VPN before the connection and to close it after the session, see Open a VPN before connect.
Set up the scripts
- Open the server in the editor (
Edit). - In
Script before connecttype the path of the script, or clickSelectto pick the file. Arguments may follow the path, for exampleD:\scripts\before.ps1 -Vpn home. -
Click
Test: 1Remote tells you what it is going to run, runs the script and shows its exit code.

-
Do the same for
Script after disconnected. - Click
Save. From now on the scripts run every time you connect to this server.
How the scripts are run
- The working directory of a script is the folder the script file is in.
- A
.ps1file is started withpowershell.exe -File, a.pyfile withpython(so Python has to be in thePATH), a.bator.cmdfile directly. Script before connect: 1Remote waits until the script has ended. Exit code0means "go on and connect"; any other code stops with the messageScript ExitCode = <code>, connection abort!. TickHide script window(not recommended for script take long time)to run the script without a window; the tooltip warns you because you would not see a script that hangs. TheTestrun always shows the window.Script after disconnected: runs when the session ends, that is when you close its tab (or, for a program that runs unhosted in its own window, when that program has exited). 1Remote does not wait for it and ignores its exit code. Its window is hidden; only theTestrun shows it.
Tip
If Windows refuses to run your .ps1 file with a message about the execution policy, run this once in a PowerShell window:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
Environment variables
Every script gets these variables, so it knows which server and which session it is about:
| Variable | Content | Example |
|---|---|---|
SESSION_ID |
A number that identifies the session. It is the same in the before and the after script of one session, and different in the next session. | 16312252 |
SERVER_ID |
The internal id of the server. It stays the same when the server is renamed. | 01GWJXAXUOBERQPARPSGKYKWBWQ |
SERVER_NAME |
The name of the server as shown in the list. | NAS |
SERVER_HOST |
Address and port of the server, separated by a colon. | 192.168.0.130:22 |
SERVER_TAGS |
The tags of the server, separated by commas. | home,nas |
In PowerShell you read them as $env:SERVER_NAME, in a batch file as %SERVER_NAME%.
Example script
The following PowerShell script shows all variables, writes them to a file and ends with exit code 0, so the connection goes on:
$sessionID = $env:SESSION_ID
$serverID = $env:SERVER_ID
$serverName = $env:SERVER_NAME
$serverHost = $env:SERVER_HOST
$serverTags = $env:SERVER_TAGS
Write-Host "SESSION_ID: $sessionID"
Write-Host "SERVER_ID: $serverID"
Write-Host "SERVER_NAME: $serverName"
Write-Host "SERVER_HOST: $serverHost"
Write-Host "SERVER_TAGS: $serverTags"
Set-Content -Path "temp.txt" -Value "SESSION_ID: $sessionID`r`nSERVER_ID: $serverID`r`nSERVER_NAME: $serverName`r`nSERVER_HOST: $serverHost`r`nSERVER_TAGS: $serverTags"
# Things you may want to do:
# if ($serverTags -like '*HOME*') { <# open VPN 1 #> }
# if ($serverTags -like '*WORK*') { <# open VPN 2 #> }
# do something that takes time, like opening a VPN
Start-Sleep -Seconds 3
# a non-zero exit code stops the connection
exit 0