ever since wvd became generally available adoption has been great but
one thing that we get asked continuously is how do I control the scale of my
environment and how do I control the cost we have many systems that are being
built to accommodate our users at full load but then in the middle of the night
for example we don't need all these VMs to be running so it'd be nice if we
could power them off when we don't need them and then have them turn on when we
will be needing them or even better on demand so that we can meet our users
where they are and while we're at it let's build this on a sure's platform
tools so that we can keep the cost down as well as make it very efficient so for
the next few minutes join us as we talk about Windows virtual desktops scaling
automation
I'm Dean Cefola and this is the Azure Academy scaling up a wvd environment is
really not a new thing we've had a feature that could do this for quite a
while that was a solution built on VMs running scripts and because we needed
that host system to be running pretty much 24/7 then we were cutting costs by
turning beams off and then increasing costs by running this other solution so
the product group took a lot of customer input went back to the drawing board and
redesigned the solution built on top of a sure automation and logic apps so our
story today begins with our documentation though from the main page
of our Doc's we'll click on products and then we'll scroll all the way to the
bottom of the screen and click windows virtual desktop and over here in the
filter on the left we're gonna type the word scale and we'll click the link for
scale session hosts automatically and from here we're gonna be able to
download all the code but first we got to take a quick look at the prereqs
and basically you need to have wbd already built as well as you need to
have contributor access on your Azure subscription then from the machine we're
gonna set this up from we need to have powershell 5.1 or later as well as the
azure AZ powershell modules once you have those in place our next step in the
process is to create an azure automation account and we do have all the code
right here so that you can download that and this does go out to the official
github repo for wvd so let me show you that quickly here and this will be
linked in the description down below so you can easily get to it and here in the
repo we'll scroll down to the wvd templates folder and inside there will
open the wvd scaling script and this is where all of today's code is coming from
just so you know where it is in case you need it for future reference so back in
the docs we're gonna download some files from that repo and then we're of course
going to run them so we'll have some parameters here that we have to fill out
as we run that script once we're done with that we're going to have to create
an azure automation run as account and the steps for that are here in the docs
as well and then once we're done with that we'll have to set up a logic app
and of course the same kind of thing as we did for as your automation will
download and run that so step number five here
has all of the input parameters that you'll need and then you'll just execute
this code block and you'll see how all that works so step one we need to
download our code and get an azure automation account built so let's go to
PowerShell so we're logged onto one of my VMs and we've got some variables here
at the top I just want to walk you through those quickly we have here our
Azure Active Directory tenant ID and if you don't know where to find that let me
show you in the azure portal so in the azure portal inside Azure Active
Directory we scroll down in the blade to properties and there is our directory ID
and then our second variable is our subscription ID and we will copy that
over as well then we need the name of our wvd tenant which host pools will be
talking to where we want the solution deployed will be this RG name variable
the logic app will run every 15 minutes we're beginning our peak time of the day
at 9:00 a.m. and ending at 6:00 p.m. and we need to set our time zone here and
this is difference from UTC time now I'm in the Eastern time zone but I'm setting
this now and we'll be running a test changing this in the process later the
session threshold per CPU has to do with configuring the maximum number of
sessions per CPU and that's going to be a threshold to determine when new VMs
need to start up during peak hours and the minimum number of our DSH here has
to do with the minimum number of session hosts will keep running during these
off-peak hours since this is just the lab I'll be giving my users 30 seconds
to log off and I'll be providing them a message title and body just telling them
you're going to be logged off and save your work we'll be deploying this in the
east us and we've got the name of our azor run as account and our automation
account here and the maintenance tag name is kind of interesting this is
where you can set a tag on the BMS that you don't want to be managed by the
scaling solution and anyone with this tag we will just ignore those resources
and then this is not part of the default the way that this script is run you sort
they don't need to do this but I also want to add a log analytics workspace on
top of this solution so that I can keep track of all of that data so I'm
providing the name of my log analytics workspace and I'm also going to do a get
operation here so I can pull that workspace in and then parse out the ID
and the key from that workspace and then I'll be switching my path here to the
local C Drive in the temp directory where I can download these files and
that's the next step so we'll just run those to download the code and then of
course we can do an LS and see that they have it downloaded let's clear the
screen here and then we come to the first script that we're going to run and
this is the create automation account script we'll just run this PowerShell
command that'll grab the particular variables that are all required as well
as our workspace and then one of the outputs that we're going to need from
this is our web hook so we'll be grabbing that as a variable and then
using that later on let's run that section of the code
and while that is running you can see the several operations are going on
import some modules yet our context and set that and get our role assignment
here and we have our output of our webhook uri so the next section here
talks about creating the run as account and we'll go back to our resource group
and open the automation account and in automation we'll scroll down in the
blade to our run as account and then we'll create an azure run as account and
hit the create button and that just takes a moment once that's complete
we'll click on the run as account and then we need to copy the application ID
now that we've copied that the next step in our Docs is to take that application
ID for our run as account and we need to grant it contributor permissions inside
Windows virtual desktop so let's go back to you PowerShell and I've got that next
step setup right here so we'll run that whole step and it will ask us to provide
the application ID which we can just paste and you can see that that service
principled account now has the role of RDS contributor so now we need to add
our webhook data into our run book and we'll go to our web hook and open the
autoscale web hook and go to parameters and we have to input the web hook data
here and we get this from our PowerShell script and here's the web hook variable
which is an output of our last script and we'll just paste that value right
here into the web hook line and hit OK and then hit save let me run the last
section of the script here so that ran in just a few seconds so let's go back
to our resource group and we can see now we have our automation account our logic
app along with our WVD Scaling run book so if we were to go to our automation
account we see that a job is already run and it has completed and we can also
take a look at the logic app itself and we see at the bottom that the logic app
is in a running State if we click on that and we can see our input frequency
is every 15 minutes and we see our web hook is in a waiting State and that is
because we have to wait 15 minutes for the next interval here but we can see
what the logic app is doing if we go to logic app designer and this is where we
can adjust the interval if we want to and we can see our webhook data here
that has our host pool subscription information time interval log off
messages all of that down here so if you need to make changes going forward you
can do that as well all right and it's been a little while
since we started this up and you can see we've gone up to 96 jobs run and you can
see that they're in completed state to see the details of the job we have to
click on it and then we can check the output first thing in the process here
is we're going to authenticate into RDS and then it's going to analyze the
script according to that webhook that we just looked at and it's going to
determine if we're in peak or off-peak hours and then adjust the environment
accordingly so if we take a look at our VMs we can see that all three are in a
running State and we are currently in off-peak hours and that's because I set
our time zone at +5 from UTC and when we refresh we can see here that another job
has queued up to run so clicking on our job again we can see if we look at the
all logs section the entire work flow and that first starts off with
authentication and then determining if we are in peak or off-peak hours then
we're gonna run that job against the hosts pool so first we check for the VMs
heartbeat and make sure that everything is working and we've got three on line
so we're going to stop two of our BMS so it's issued the shutdown command and now
we're waiting for the VM to respond and there's the command for the second VM to
shutdown and if we go back to the VM screen and refresh we can see that first
VM has shut down so in another moment we'll have our second BM shutdown so if
we go back to the job and we refresh the job status the job is now completed so
now what we want to do is we want to switch it up a little bit so we're gonna
go to our logic app and we're gonna open that up and then we can edit the logic
app here at the top by clicking edit and then we can click to edit either one of
these properties so first I'll adjust the time interval from 15 minutes to 5
and then we'll click Save again now in the webhooks section of our logic app
what we want to do is change our time zone so our peak hours and end peak
hours are just fine and we just need to alter the time zones so that our system
knows that right now all of our BM should be up and
running and as I said earlier the +5 here was a setting that I was going to
configure but that's not Eastern Time so I need to change this from a plus 5 to a
minus 5 and then we'll click the Save button at the top and then rather than
wait for the job to kick off again we can just click the Run button up here
which will just start it right away then we can go back to our automation account
and watch this in action and under jobs we see that our job is currently
starting click on that and we'll click on the all logs again and just like
before the process here has our authentication happening first and then
we can see that it is peak hours now and we need to start our session hosts so we
see that our VM check shows that BM 0 has no heartbeat 1 has no heartbeat but
2 is online the current number of hosts is 1 so with one host we have the
capacity for four users I'm going to open multiple wbd sessions and then
we're going to get capacity to increase so here in the management tool you can
see I've got five users and then I have already got one active session for the
WBT admin here and we need to get a couple more going so I'll open the task
manager yes so we can see which users are logged
on now we've got five users logged on to this single VM and so what we would
expect now is that the solution will detect this the next time that it runs
and scale up some more VMS for us and so looking at our previous job that just
finished running a few minutes ago we can see that the number of total users
logged on was one capacity was four so there was no reason to do anything at
that time but this time we have a new job queuing up and it is now running and
the output here says total number of user sessions currently as five capacity
is four so we need to turn on another VM to meet demand we go back to the VM
screen and hit refresh and we can see VM zero is now running so that means that
the next user who goes to login we should get them logging on to a
different beat session let me go back to my wbd
management and I'll add another user here and we'll add Wonder Woman and
there we go so we can log in with her now everyone on the left is logged on to
box number two and Wonder Woman is on box zero the hope that you've enjoyed
looking at the new scaling automation for wvd and what it can do for you in
your environment help you to manage those logon storms peak hours off-peak
shut your VMs down when they're not going to be needed so that you can get
the best bang for your buck out of wvd as you move forward and if you like this
video please do click on the thumbs up as well as the subscribe button join us
here at the Azure Academy community and to stay informed and when our new videos
come out which is roughly once a week you can hit that notification bell as
well so please leave us some comments feedback down below on what you think
about the scaling script as well as any feature requests that you have that we
can relay to the wvd product group thanks very much for joining us today
and we'll see you in the next video happy learning