> 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

Ejecute un script de PowerShell antes de abrir una conexión en Remote Desktop Manager para modificar las propiedades de la sesión o cancelar el inicio.

Es posible ejecutar un script de PowerShell antes de establecer una conexión, cancelar el inicio 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 inicio de la sesión. Cuando se cierra la sesión, las propiedades vuelven a sus valores originales.

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 que se proporciona a continuación y haga clic en ***Aceptar***.

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

### Cancelar el inicio de la sesión

Para cancelar el inicio 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 Devolutions PowerShell

El módulo Devolutions PowerShell 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
