Monday, March 16, 2009

RDP powershell script from Alex Pavlovsky

Ever been tired of ping –t, then waiting until the remote desktop is available when rebooting servers? No more! Save the below script as a .ps1 file (for example, c:\connect.ps1), and run the following command:

C:\connect.ps1 –server servername

When the server comes online and is reachable via terminal services, the TS connect window will automatically pop up. No more guessing when it’s ready!

param(
$server = $(Read-Host "Enter the server number")
)
$ping = new-object System.Net.NetworkInformation.Ping
do {$testPing = $ping.Send($server)} while ($testPing.status -ne "Success")
write-host "Ping successfull, trying connection on 3389"
do {$socket = new-object System.Net.Sockets.TCPClient($server,3389)} while ($socket.Connected -ne $true)
write-host "Port 3389 listening, connecting MSTSC"
mstsc -v $server

No comments:

Post a Comment