> 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/it/knowledge-base/how-to-articles/execute-a-powershell-script-before-opening-a-connection.md).

# Eseguire uno script PowerShell prima di aprire una connessione

È possibile eseguire uno script PowerShell prima di stabilire una connessione, annullare l'avvio e persino importare script PowerShell esterni. Per eseguire queste azioni, occorre andare nelle ***Proprietà*** della voce in ***Eventi*** – ***Prima dell'apertura*** e scegliere ***Esegui PowerShell*** nel menu a discesa più in alto.

Utilizzando la proprietà $RDM.connection, lo script modifica temporaneamente una qualsiasi delle proprietà della sessione solo nel contesto dell'avvio della sessione. Quando la sessione viene chiusa, le proprietà tornano ai valori originali.

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

Di seguito sono riportati alcuni esempi di script che possono essere eseguiti in questo modo.

### Aggiungere la data al titolo della scheda

Nella finestra ***Script PowerShell***, copi lo script fornito di seguito e clicchi su ***OK***.

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

### Annullare l'avvio della sessione

Per annullare l'avvio di una sessione, nella finestra ***Script PowerShell*** della voce, copi lo script seguente e clicchi su ***OK*** per salvare.

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

### Eseguire cmdlet dal modulo PowerShell di Devolutions

Il modulo PowerShell di Devolutions può essere utilizzato per eseguire cmdlet in voci specifiche tramite il comando `Import-Module`.

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

In questo esempio, lo script carica DnsClient tramite `Import-Module`, cerca l'indirizzo IP per il nome host della voce e lo scrive nel campo ***TeamViewer ID***.

### Importare uno script PowerShell esterno

Il cmdlet `Import-Module` può essere utilizzato per richiamare uno script PowerShell esterno.

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

In questo esempio, il cmdlet `Import-Module` carica lo script RSAToken.ps1 e richiama la sua funzione New\_RSAToken, che restituisce un valore. Successivamente aggiunge il contenuto della variabile $RSAToken alla password della sessione prima di aprire la sessione.


---

# 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/it/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.
