Windows Virtual Desktop Host Pool Automation Part 3

Windows Virtual Desktop Host Pool Automation Part 3

Hello together,

it´s been already a few weeks since I´ve released my last article and a lot of great things happened – especially in terms of Autoscaling for our WVD HostPools!

Since last week I´ve been extensively working on a consolidated solution, which is easy to use and supports existing Spring Update Windows Virtual Desktop Host Pools and finally – here it is! This solution combines the best of Microsoft´s scripts, with user interfaces and one single script to rule them all 🙂

The WVDLogix Autoscaling Tool in Preview!

Download it from my GitHub Repo (info, you just need to download the Powershell):

To help you getting started with my script I´ve created a video for you which explains the steps in detail. Please find it here:

Really easy to use right? – Here you can find the detailed documentation


To start with the script the following prerequisites must be fulfilled:

  • Have an Azure Subscription and Tenant properly configured
  • Have a first Host Pool (the number of machine doesn´t matter)
  • Create a Resource Group upfront for your automation resources (to ensure proper separation from other resources)
  • Have the Az Powershell Modules installed

Next, you need to download the Powershell script from GitHub using the link above, or by clicking here.

I´ve stored the script in my preferred location, this is C:\WVD\Autoscaling in my example.

Once this is done, all you need to ensure is to start Powershell with elevated rights (Administrator). You can do this by right clicking on the Powershell icon from your startmenu and select “Run as Administrator”.

Now we´re going to navigate to the location were we´ve stored the script in. For me this is C:\wvd\Autoscaling so I switch the directory to it.

cd C:\wvd\Autoscaling

Now I can run the script by typing:


The first thing you need to do is to login to your Azure tenant with administrative permissions.

Now, the Autoscaling Tool will ask you to specify a download folder for the working files and scripts by Microsoft, which will be used to finalize the setup process. In my case I select the same folder, where my actual script is and hit enter.

You will see that three more scripts will be downloaded to your computer, let me explain their purpose and in the order they´re executed.

  1. CreateOrUpdateAzAutoAccount.ps1
    This one will create the Automation Account and Runbook in your environment and import all the required modules you need to run the Autoscaling
  2. New-RunAsAccount.ps1
    To run operations on the virtual machines it´s mandatory to have a “run-as account” in place. As this tool should give you a single place for all operations required, this tool will create this Account for you and provides you with everything you need.
  3. CreateOrUpdateAzLogicApp.ps1
    This script is going to create the Logic App in your environment and fills it with the parameters which fit best for your environment. You will see the parameters in detail when we continue.

To make it easy for new users with Powershell / WVD / or Azure in general, I´ve tried to built in as much graphical interfaces as I could (a real GUI will come in the next version).

But for now we can select a Resource Group to place our automation resources in:

This GridView will present you all RGs available in your tenant with your subscription. I select the one I´ve created upfront, which is called KCLD-WE-Automation and click “OK” on the lower right corner to continue.

Now I can select a location for my resources, this will be represented as GridView as well. Please select your option and hit the OK button.

If you want to benefit from gathered logs, you have the possibility to send all those logs to your LogAnalyticsWorkspace. The next question gives you the opportunity to type in the name of your LogAnalyticsWorkspace. This is an optional feature! Just leave it empty and hit enter to continue.

Now the tool is asking you for a secure password, which will be needed to sign the “run-as account”. Please provide a secure password here and hit enter.

And now the tool will create the Automation Account, Runbook and “run-as account” for your environment.

ATTENTION! This process can take between 15-20 minutes! There is no manual action needed in between.

In some cases, especially if you used this script already, it might be possible that you receive an error message at the level of “run-as account” creation. The tool will remediate this issue by itself while informing you that “New-AzRoleAssignment: The role assignment already exists”.

Once this process is done, we need to configure our LogicApp with some parameters, which need to be copied from a few lines above.

Please copy the WebHookURI, represented by the agent and mark it with your mouse and make a right click to copy it. Afterwards paste it in the field below:

The highlighted area represents the WebHookURI – make sure that there is no space in between

Next you need to specify a Tag to exclude resources from being up- and downscaled! This is a great solution in case you want to have a machine 24/7 up and running. You can define this Tag from the Azure Portal or Powershell and just type the value here. In my case I leave it as is and hit enter.

Now, a new window will open, which allows you to select your HostPool. If you have already multiple HostPools, select the one, which shall be automated. Currently there can only be one HostPool automated by the script at a time – you would need to run the script again to also manage another HostPool.

Now you will be prompted for the following parameters in a row. The I will explain each value in brackets!

  • Please fill out how often the job shall be repeated, values in minutes, e.g. ’15’ (defines the recurrence interval – can be changed later on)
  • Enter the start time for peak hours in local time, e.g. 9:00 (Defines when the automation should start)
  • Enter the end time for peak hours in local time, e.g. 18:00 (Defines when the automation should stop)
  • Enter the time difference between local time and UTC in hours, e.g. for CET: +2:00 (Defines the timezone, for CET it´s +2:00)
  • Enter the maximum number of sessions per CPU that will be used as a threshold to determine when new session host VMs need to be started during peak hours ( Defines the maximum number of sessions per CPU, to determine when new Session Hosts should be started)
  • Enter the minimum number of session host VMs to keep running during off-peak hours (Lets you define the resources, that you need to allow computing after the business hours)
  • Enter the number of seconds to wait before automatically signing out users. If set to 0, any session host VM that has user sessions, will be left untouched (Allows you to give users time before their sessions will be disconnected in case a downscaling occurs)
  • Enter the title of the message sent to the user before they are forced to sign out (The logoff message title)
  • Enter the body of the message sent to the user before they are forced to sign out (The logoff body message)

And finally, when everything has configured well, you will see the the logic app scheduler is successfully created.

And finally that´s it! The Autoscaling script has been successfully configured and is able to do it´s job.


In case, that some values aren´t correct or you just want to modify the scheduling / recurrence or any other value, you can run the script again, or navigate to the Azure Portal, into the resource group, where you´ve created all the automation resources and select the Logic App.

On the left-hand side select “Logic app designer”

And now you can see in the center of the screen all values that you are able to change, in case a modifcation is needed.

Click on save in the top left corner and your changes will be immediately active!

I really hope this helps you out there to get started with automation! At this stage, I also want to thank Microsoft for sharing the initial version of the script (Links can be found in the script), which has been modified and consolidated in my tool and which will be used by me to get extended functionalities.

I´m really looking forward to your feedback!



Please follow and like us:

14 Replies to “Windows Virtual Desktop Host Pool Automation Part 3”

  1. Hi,

    This looks awesome. However it just seems to error
    “C:\WVDAuto\CreateOrUpdateAzAutoAccount.ps1 : Cannot validate argument on parameter ‘Name’. The argument is null or empty.”

    I’ve added the necessary details when prompted

  2. Dear Jamie,

    thanks a lot for your feedback. Please provide me with more detailed information or maybe a screenshot! I would really like to help you get it running.

  3. Hi Patrick,
    great guide and script! In case you are working with multiple subscriptions you can add:
    “Get-AzSubscription | Out-GridView -PassThru | Select-AzSubscription”
    to select the subscription at the beginning.

    Best regards

  4. Hi,
    I have used your script but I have a question. it seems that it is not stopping the VM’s if session count drops below threshold limit. VM’s should be stopping if there are no user session running? Correct.

  5. Basically looking at below details shouldn’t it be stopping VM’s?
    2020-07-13 15:56:02 [434] Number of session hosts in the HostPool: 2

    2020-07-13 15:56:02 [456] Using current time: 2020-07-13 15:56:02, begin peak time: 2020-07-13 08:00:00, end peak time: 2020-07-13 18:00:00

    2020-07-13 15:56:02 [460] In peak hours

    2020-07-13 15:56:02 [489] Get all VMs, check session host status and get usage info

    2020-07-13 15:56:04 [510] Session host: ‘’, power state: ‘VM running’, status: ‘Available’, update state: ‘Succeeded’, sessions: 0, allow new session: True

    2020-07-13 15:56:04 [513] Get all VM sizes in location: eastus

    2020-07-13 15:56:05 [510] Session host: ‘’, power state: ‘VM running’, status: ‘Available’, update state: ‘Succeeded’, sessions: 0, allow new session: True

    2020-07-13 15:56:05 [560] Number of running session hosts: 2 of total 2

    2020-07-13 15:56:05 [561] Number of user sessions: 0 of total allowed 4

    2020-07-13 15:56:05 [562] Number of user sessions per Core: 0, threshold: 2

    2020-07-13 15:56:05 [563] Minimum number of running session hosts required: 0

    2020-07-13 15:56:05 [676] No need to start/stop any session hosts

    2020-07-13 15:56:05 [677] End

  6. Hi Patrick. Great additions to the script. can I ask does this deployment now work with ARM deployments? W have recently redeployed our clients to the new ARM version and are awaiting updates from microsoft to be able to use the same shutdown scripts in this new environment.

  7. Enter the maximum number of sessions per CPU: Are you able to enter a fractional number? My use case for WVD is with MS Teams which is very CPU intensive. I’m looking at something like .31 users per VM because I’m using a 16 core VM and want a max of around 5 users.

Leave a Reply

Your email address will not be published.