Extending Remote Desktop Services via PowerShell – Part 4

(Post courtesy Manoj Ravikumar Nair, who you can follow on his excellent blog at https://www.powershell.ms)

Previous Post: Extending Remote Desktop Services via PowerShell – Part 3

Configuring a RDS Farm using PowerShell

So far, we have installed two RD Session Host Servers, added some RemoteApps, configured the Session Host Servers, installed the Web Access Server, installed and configured the Connection Broker.

We are all set to create a RDS Farm. I have already created the related DNS records (RDSFARM) as shown in the screenshot below:

clip_image002

Let’s connect to our RD Session Host Server COLFAX and navigate to the ConnectionBrokerSettings Container

clip_image004

Before we start creating a FARM, we need to figure out the corresponding value of the FARM membership as defined in the ServerPurpose Property.

As you can see in the screenshot below, the value 3 corresponds to the Farm Membership.

dir .\ServerPurpose | fl *

clip_image006

We also need to the IP address of the NIC attached to the Session Host Server (RedirectableAddresses)

clip_image008

To create a FARM, we can run the following command

Set-Item ServerPurpose -value 3 -ConnectionBroker <FQDN-OF-RD-CONNECTIONBROKER- GOES-HERE> -FarmName <FQDN-FARM-NAME-GOES-HERE> -IPAddressRedirection 1 -CurrentRedirectableAddresses <IP-ADDRESS-YOU-WANT-TO-USE-GOES-HERE>

Since, we have only one NIC card attached to each of the RD Session Host Servers, the value of the CurrentRedirectableAddresses can be substituted by the Name Property of the RedirectableAddresses as shown in the figure below

clip_image010

In the above case, we are storing all the contents of the RedirectableAddresses container into a PowerShell variable called $currentaddress.

Now, let’s take a look at the members of the $currentaddress

clip_image012

Notice that $currentaddress is a Custom TSObject and has the Name as a Property. To just display the contents of the Name Property, run $currentaddress.Name as shown below

clip_image013

Now, let’s re-examine the original syntax for creating the FARM

Set-Item ServerPurpose -value 3 -ConnectionBroker <FQDN-OF-RD-CONNECTIONBROKER- GOES-HERE> -FarmName <FQDN-FARM-NAME-GOES-HERE> -IPAddressRedirection 1 -CurrentRedirectableAddresses <IP-ADDRESS-YOU-WANT-TO-USE-GOES-HERE>

Based on our current environment, the values for the Parameters above would be as follows:

-ConnectionBroker - Liberty.powershell.ms

-FarmName – RDSFARM.powershell.ms

-IPAddressRedirection – 1 {this implies that IP address redirection is enabled}

-CurrentRedirectableAddresses - $currentaddress.Name {this implies that the value of the CurrentRedirectableAddresses can be obtained from the Name Property of the RedirectableAddresses Container}

So here is the logic that we would apply

1) Connect to the RDS PowerShell Drive

2) Store the value of the Name Property of the RedirectableAddresses in a PowerShell variable called $currentaddress

3) Run the base script for creating a farm by substituting the values

Step 1:-

clip_image015

Step 2:-

clip_image017

Step 3:-

clip_image019

That’s all about it. Let’s check the properties of the RD Session Host Server.

clip_image020

clip_image021

Now, you must be wondering why did I take the pain of storing the value of the IP Address into a variable and not just directly assign it to the CurrentRedirectableAddresses. Assigning the values directly will work for one or two servers, but when you want to add say about 10 servers into the FARM, you will have to go to each server and run this script.

Wouldn’t it be easier, if we could get a list of computers, pass it as an input to a script that will connect to each computer in the list and configure the FARM settings.

It is a pretty simple script as given below. Here, I assume that you have saved the computer names, one per line, into a Text file called Servers.txt in the C:\Scripts Directory

Get-Content C:\Scripts\Servers.txt | Foreach-Object {

$session = New-PSSession –ComputerName $_

Invoke-command –Session $session –ScriptBlock { ipmo RemoteDesktopServices;$currentaddress = dir –Path RDS:\RDSConfiguration\ConnectionBrokerSettings\RedirectableAddresses; Set-Item –Path RDS:\RDSConfiguration\ConnectionBrokerSettings\ServerPurpose –Value 3 –ConnectionBroker ‘Liberty.powershell.ms’ –FarmName ‘RDSFARM.powershell.ms’ –IPAddressRedirection 1 –CurrentRedirectableAddress $currentaddress.name}

Get-PSSession | Remove-PSSession

}

Now that we have our script ready, I just removed the Colfax server from the FARM Membership as shown below:

clip_image023

In this way, I can test whether the script is working as expected. I also created a text file called Servers.txt which contains the names of our two RD Session Host Servers. PowerShell Remoting has been enabled on both the Session Host Servers.

clip_image025

Now, it’s the time to run the script. I have changed the default script execution behavior of PowerShell to Unrestricted

clip_image027

Okay, we have a good start. The Script ran without errors and returned the cursor back on the next prompt. But did it do what it was supposed to do?

Let’s take a quick look at the RD Connection Broker Properties of both, COLFAX and FUJI.

COLFAX

clip_image029

FUJI

clip_image031

Yipeeeeeeee!!, it worked as expected. J This is the ‘Power of PowerShell’. By just using a simple logic, and a few lines of code, we were able to automate the creation of a RDS FARM.

Next Post: Extending Remote Desktop Services using PowerShell – Part 5