Execute a PowerShell script before opening a connection

It is possible to execute a PowerShell script before establishing a connection, to cancel the launching, and even to import external PowerShell scripts. To accomplish these actions, you would need to go into the Properties of the entry under EventsBefore opening, and choose Execute PowerShell in the topmost dropdown menu.

Using the $RDM.connection property, the script temporarily changes any of the session’s properties in the context of the session launch only. When the session is closed, the properties revert to their original values.

An RDP entry's Event section, with a PowerShell script set to add the current date to the tab's title
An RDP entry's Event section, with a PowerShell script set to add the current date to the tab's title

Below are some examples of scripts that can be executed in this manner.

Add date to tab title

In the PowerShell script window, copy the script provided below and click OK.

$date = Get-Date -DisplayHint Date
$RDM.Connection.TabTitle = $RDM.Connection.Name + "(" + $date + ")"

 

Cancel session launch

To cancel the launch of a session, in the PowerShell script window of the entry, copy the script below and click OK to save.

$RDM.Cancel = $true

Import external PowerShell script

The Import-Module cmdlet can be used to call an external PowerShell script.

Import-Module c:\Temp\RSAToken.ps1
$RSAToken = New_RSAToken
$RDM.Connection.RDP.password = $RDM.Connection.RDP.password + $RSAToken

In this sample, the Import-Module cmdlet loads the RSAToken.ps1 script and calls its function New_RSAToken, which returns a value. Then it appends the content of the $RSAToken variable to the session’s password before opening the session.

 

Devolutions Forum logo Give us Feedback