Wednesday 18 November 2009

Use remote desktop connection faster with PowerShell

We have more than ten servers in our production network and all of them are accessible via RDC. The problem is that to connect via RDC you have to specify public IP and not local network nice name like "prod-01" or "prod-db-01". I had not bothered about it till I had a need of connecting some of them several times a day. To solve this problem I wrote a PowerShell script that takes nice name of server and launches RDC with its IP:

$prodServers =
@{
'prod-01' = '1.2.3.2';
'prod-02' = '1.2.3.3';
'prod-03' = '1.2.3.4';

'prod-db-01' = '1.2.3.6';
'prod-db-02' = '1.2.3.8';
}

Set-Alias rdcx 'c:/WINDOWS/system32/mstsc.exe'
function rdc([string]$serverName)
{
$param = ''

if ((![System.String]::IsNullOrEmpty($serverName)) -and ($prodServers.Contains($serverName)))
{
$param += '/v:' + $prodServers[$serverName]
}

rdcx $param
}

It's extremely useful if you have many servers, just believe me :)

1 comment:

Jesse Jones said...

I'm a new employee and I'm sick of typing in my username and password into 2 different RDP windows to access 2 different sessions every morning. Not only that, but they always disconnect after a few minutes.

How can I use powershell to log in for me automatically (with the click of a button) to each one?