RDP to Your Server Through PowerShell

And never have to remember random server names again…

In the course of maintaining our web apps, I occasionally find I need to remote into one or more of our servers. Since I’m typically already on a PowerShell command line it just makes sense that I’d want to fire up my RDP (Remote Desktop Protocol) session right there. That’s not hard, a little googling turns up an RDP script that has already been written.  It’s called Start-RDP and it does exactly what it says.

I add that script to my PowerShell profile and now I can easily say:

start-rdp "fpwebwww01"

from the command line which launches the RDP session and I am good to go.

Launch RDP session

Nothing special there, certainly nothing worthy of its own blog post. So, let’s get to the good stuff…

The thing that’s bothering me is that name, “fpwebwww01”. Who can remember that? Not me. Additionally some servers I can only get to via their IP.  No way am I going to remember all those.  What if I could give them friendlier names, names that mean something to me? And how about letting me auto complete those names with standard command line auto completion?

So that’s exactly what I did.

Introducing PowerShell Validated Parameters

That kind of thinking led me to PowerShell’s “validated parameters” blog by Ed Wilson.  Simply put, it’s a way to declare a PowerShell function such that the parameter value must be from a specific list. Like this:

powershell-auto-complete

I’m just tabbing through the choices right there. Pretty handy I think.

The implementation is this simple wrapper around Start-RDP. The wrapper function is called ‘rdp’ and uses a validated parameter set.

function rdp {
param([ValidateSet("old_www_box","dev.fpweb.net","ampdev.net","www1","www2", "mercury.fpweb.net")][string]$server)

$the_server = $server # $server can only be one of set values.

if($server -eq “www1”) { $the_server = “172.27.0.53” }
if($server -eq “www2”) { $the_server = “172.27.0.67” }
if($server -eq “old_www_box”) { $the_server = “204.144.122.42” }
Start-RDP -Server $the_server -Fullscreen
}

Let’s break this function down.  First part is the parameter declaration.

param([ValidateSet("old_www_box","dev.fpweb.net","ampdev.net","www1","www2", "mercury.fpweb.net")][string]$server)

$server is the parameter name.  The [ValidateSet()] is where the magic happens. That says “restrict the parameter to one of the values in this list”. PowerShell will provide auto completion using the contents of that list. And that list is the names I use in my brain when I think of different servers around here.

dev.fpweb.net is my team’s build server. mercury.fpweb.net is an internal customer management app. I can RDP directly to those. The others are special cases – www1 and www2 are not real DNS entries for example. That’s just what I call them when I am thinking of the two servers in our load balanced setup that powers our website, www.fpweb.net. For those, I’ve added a bit of logic that swaps out my friendly name for the actual IP.

That’s all there is to it. Modify this function for your own environment, then drop it into your PowerShell profile and it will always be available when you need it. Hope that helps!

VN:F [1.9.22_1171]
Rating: 5.8/10 (4 votes cast)

About Fpweb.net Crew

Our business is centered on bringing enterprise-class strategy, support, and security to your hosted or managed platforms no matter where you choose to deploy your environment. We specialize in providing managed services, cyber security, and expert, USA-based, 24/7 Absolute Support® on-premises, or in any cloud.
This entry was posted in PowerShell and tagged , , , , , , , , , . Bookmark the permalink.

One Response to RDP to Your Server Through PowerShell

  1. Dexter (@DexterPOSH) says:

    Cool use of the [ValidateSet()].
    I would suggest rather than putting multiple If {} statements, rather go for the switch {}. It will be a lot easier to manage it when the list grows.

Leave a Reply

Your email address will not be published. Required fields are marked *

Let's make sure you're human first: *