> For the complete documentation index, see [llms.txt](https://docs.devolutions.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.devolutions.net/rdm/es/knowledge-base/how-to-articles/execute-a-powershell-script-before-opening-a-connection.md).

# Ejecutar un script de PowerShell antes de abrir una conexión

Es posible ejecutar un script de PowerShell antes de establecer una conexión, cancelar el lanzamiento e incluso importar scripts de PowerShell externos. Para llevar a cabo estas acciones, debe ir a las ***Propiedades*** de la entrada, en ***Eventos*** – ***Antes de abrir***, y elegir ***Ejecutar PowerShell*** en el menú desplegable superior.

Mediante la propiedad $RDM.connection, el script cambia temporalmente cualquiera de las propiedades de la sesión únicamente en el contexto del lanzamiento de la sesión. Cuando se cierra la sesión, las propiedades vuelven a sus valores originales.

![](https://cdnweb.devolutions.net/docs/RDMW4363_2025_2.png)

A continuación se muestran algunos ejemplos de scripts que se pueden ejecutar de esta manera.

### Añadir la fecha al título de la pestaña

En la ventana ***Script de PowerShell***, copie el script proporcionado a continuación y haga clic en ***Aceptar***.

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

### Cancelar el lanzamiento de la sesión

Para cancelar el lanzamiento de una sesión, en la ventana ***Script de PowerShell*** de la entrada, copie el script siguiente y haga clic en ***Aceptar*** para guardar.

```powershell
$RDM.Cancel = $true
```

### Ejecutar cmdlets desde el módulo de PowerShell de Devolutions

El módulo de PowerShell de Devolutions se puede utilizar para ejecutar cmdlets en entradas específicas mediante el comando `Import-Module`.

```powershell
Import-Module DnsClient
$RDM.connection.TeamViewer.ID = (Resolve-DnsName -Name $HOST$).IPAddress
```

En este ejemplo, el script carga DnsClient mediante `Import-Module`, busca la dirección IP del nombre de host de la entrada y la escribe en el campo ***TeamViewer ID***.

### Importar un script de PowerShell externo

El cmdlet `Import-Module` se puede utilizar para llamar a un script de PowerShell externo.

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

En este ejemplo, el cmdlet `Import-Module` carga el script RSAToken.ps1 y llama a su función New\_RSAToken, que devuelve un valor. A continuación, añade el contenido de la variable $RSAToken a la contraseña de la sesión antes de abrirla.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.devolutions.net/rdm/es/knowledge-base/how-to-articles/execute-a-powershell-script-before-opening-a-connection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
