All Articles

RDP Automatic scaling in Azure

I am moving a customer from a traditional hosting provider to Microsoft Azure. They have a traditional setup with ADDS, file servers and a RDP Session host. In their current setup they complain about performance, so i wanted to test out if we could run some kind of scaling in Azure based on time or usage.

A quick search on Google sent me towards a Technet script promising the features i was after. Let’s test it out. You will need:

  • An active Azure Subscription
  • An ADDS setup
  • A RDP Broker
  • Minimum 2 RDP session servers When you are done, you should end up with something like this: 1 I have configured a Fileserver to host the User Profile Disks, a server to host the Broker and web access RDP roles and two servers to host the session host roles.


The script is executed as a scheduled task on the RDP Broker server. 2 It will turn on/off the Session Hosts, not delete them, so there will still be a very small cost for the VM’s when they are not being used. As we all know we do not pay for compute when a server is turned off (deallocated).


I start with configuring the Connection Broker, as it will serve as the controller of operations.

Script setup

First we need to download and extract the Automatic Scaling script to c:DynamicRDSH or whatever folder you want. Open the config.xml 3 Unfortunately, there is an error in the config.xml file, i have provided a fixed version here:

<?xml version="1.0" encoding="UTF-8"?>  
    <Variable Name="AzurePublishSettingFile" Value="INSERT PATH TO PUBLISH FILE"/>
    <Variable Name="CurrentAzureSubscriptionName" Value="INSERT SUBSCRIPTION"/>
    <Variable Name="CloudServiceName" Value="INSERT CLOUD SERVICE"/>
    <Variable Name="BeginPeakTime" Value="08:00:00"/>
    <Variable Name="EndPeakTime" Value="17:00:00"/>
    <Variable Name="TimeDifferenceInHours" Value="+1" />
    <Variable Name="SessionThresholdPerCPU" Value="3"/>
    <Variable Name="MinimumNumberOfRDSH" Value="1"/>
    <Variable Name="LimitSecondsToForceLogOffUser" Value="120"/>
    <Variable Name="LogOffMessageTitle" Value="System Under Maintenance"/>
    <Variable Name="LogOffMessageBody" Value="Please save your work and logoff!"/>

Change the location of your AzurePublishSettingFile, Azure Subscription and the cloud service to the one your servers are running in.

Connecting to Azure

To install Azure Powershell simply use this Link. Unfortunately, the platform installer installs a bunch of unnecessary programs 4 Which, if you are like me, you uninstall. 5 Now we need to establish the connection to our Azure Subscription, thankfully, this is really simple. First you run


This will open up a browser and ask for your credentials, input these and when asked, save the file in your c:DynamicRDSH folder 6 Next import the PublishSettingsFile with

Import-AzurePublishSettingsFile -PublishSettingsFile

7 Now we will be able to run commands agains Azure from this server. When we are in Powershell we need to run these commands in order for the script to work. Since the script is not signed we need to allow for all scripts to run.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

We also need to unblock the script.

Unblock-File .RDSScaler.ps1

Setup schedule Next we need to setup the scheduled task that will probe the session host to see if they need scaling. Create a new task and name it whatever you like and make sure you have selected Run Whether user is logged on or not and Run with highest privileges 8 Select Triggers and create one that repeats every 15 minutes for indefinitely. If you have a small or very large enviroment you could choose to lower this setting, anything lower than 15 minutes will not allow for Azure to complete its operations. 9 Select Actions and enter powershell.exe c:dynamicrdshrdsscaler.ps1 10 When done you should see something like this 11


I was a little curious so i ran the script to see what would happen 12 It detected that it was off-hours and noticed i had 2 sessions host running and no active sessions, so it turned off a session host. RDSScale.log

10/13/2014 11:54:01 AM - [Info] Starting RDS Scale Optimization: Current Date Time is: 10/13/2014 11:54:01  
10/13/2014 11:54:07 AM - [Info] It is off-peak hours. Starting to scale down RD session hosts...  
10/13/2014 11:54:07 AM - [Info] Processing collection Fjernskrivebord


martinhannemann: I’m sorry for the bad scaling on some of the pictures, i will improve this in further posts.

Published 13 Oct 2014