How to set the network interface for RDP in Windows Server 2012

rdpremote desktopremote-desktop-serviceswindows-server-2012

I have a server with multiple network interfaces, and I need RDP to only listen for connections on one of them; the server is not a Remote Desktop server, RDP is only used for remote administration.

In Windows Server 2008 R2, I was able to configure this using the Remote Desktop Session Host console; in Windows Server 2012, that console isn't there anymore.

How can I configure this in WS2012?

Best Answer

Remote Desktop Services actually have a pretty rich WMI object library you can take advantage of to query and manipulate the configuration. As of Vista/2008, it's located in the root\cimv2\TerminalServices namespace. Here's a good place to start on browsing what's available: Remote Desktop Services Configuration classes

In regards to your specific question, I'd just like to clarify that RDP can only be bound to a network adapter, not a specific IP. I know you said "network interface". I just wanted to clarify for others who might stumble onto this question. It's a somewhat common request on machines that only have one adapter and multiple IPs. If that's what you're looking for, there are other ServerFault questions with answers more detailed. But if I recall correctly, your best bet is to just limit the connections using the built-in firewall.

The specific class that has what you need is called Win32_TSNetworkAdapterSetting. There are 3 methods associated with the class that you can use:

In my experience, SetNetworkAdapterLanaID is more reliable than SelectNetworkAdapterIP because of the "All network adapters" option. It seems like if it's currently configured to "All network adapters" it won't change to the specific adapter with the IP you specify, it will just keep it on "All network adapters" which is technically still correct.

So you're left with using SetNetworkAdapterLanaID and which requires an integer ID value as an argument to the method. So here's how you find the ID to use. First get a reference to the instance of the class. My example here will use the default terminal name called "RDP-Tcp", but it's possible (though unlikely) your systems have additional or different terminal names.

You can check the current status of what network adapter is configured with the following PowerShell:

gwmi Win32_TSNetworkAdapterSetting -filter "TerminalName='RDP-Tcp'" -namespace "root/cimv2/TerminalServices" | Select NetworkAdapterLanaID,NetworkAdapterName

In order to call a method, it's nice to have the instance of the class assigned to a variable, so let's do that:

$ts = gwmi Win32_TSNetworkAdapterSetting -filter "TerminalName='RDP-Tcp'" -namespace "root/cimv2/TerminalServices"

A handy feature of this class is that a couple of the properties it returns are lists of the possible network adapters you can use.

$ts | select -expand DeviceIDList
$ts | select -expand NetworkAdapterList

This should return two lists. The first is a 0-based list of IDs and the second is the friendly name of the adapters associated with the first list. So on my test machine, it returned:

0
1

and

All network adapters configured with this protocol
Intel(R) PRO/1000 MT Network Connection

If you want to correlate the IDs in the DeviceID property to their names in the NetworkAdapterList, you can do so like this:

$adapters = $ts | select -expand NetworkAdapterList
$device_ids = $ts | select -expand DeviceIDList

$adapter_list = @()
foreach ($device_id in $device_ids) {
  $adapter_list += @{$device_id = $adapters[$device_id]}
}
$adapter_list # Mapping of device IDs to adapter names

By default, it's set to use ID 0 which is "All network adapters configured with this protocol". So if we wanted to change it to use the Intel NIC explicitly, we just have to call the method using our existing object and the associated ID.

$ts.SetNetworkAdapterLanaID(1)

You can then verify the change by re-querying the object:

gwmi Win32_TSNetworkAdapterSetting -filter "TerminalName='RDP-Tcp'" -namespace "root/cimv2/TerminalServices" | Select NetworkAdapterLanaID,NetworkAdapterName