>> Learn how to use Azure Automation with a Windows Machine
with PowerShell in this edition of Azure Tips and Tricks.
As a developer, when I do something more than once,
I want to automate it.
Azure Automation makes this very easy for most common tasks,
like scaling and Azure SQL Database up and
down and starting and stopping a VM on the schedule.
Let's create an Azure Automation account and see how it works.
I'll add a new resource and I'll search for Azure Automation.
There it is. All right,
I want this one, "Automation".
Now, click "Create". There we go.
I need to enter in a name for the Azure Automation account.
Next, I select my existing resource group.
Okay, I'll leave this to "Yes".
This creates an Azure Run As account,
which is a service principal that has
a contributor role in this subscription.
Azure Automation uses this account to manipulate
services in my subscription like VMs.
Let's create it. This takes a minute or so to finish.
So, I'll skip to when it is done.
Here we go. This is the Azure Automation account.
There are lots of things that we can do here,
including create Runbooks and run jobs.
There is a "Runbook Gallery" that contains
pre-made runbooks for many common automation scenarios.
The first thing that we need to do is make sure that
the PowerShell modules of this account are up-to-date.
We do that by going to the "Modules" menu item.
This contains all of the PowerShell modules that we
get by default with this automation account.
By clicking on "Update Azure Modules",
I can make sure that we have the latest version of these modules.
All right, now, let's create a new runbook.
I'll go to "Runbooks" and click "Create a Runbook".
Now, I'll give it a name.
I'll pick "PowerShell" for the runbook type
as I want to run a custom PowerShell script.
You can also create runbooks with
Python or with a graphical wizard, if you want.
Let's create it. Here, I can create the contents of the runbook.
I can use pre-made commandlets and other assets.
I want to create a custom scripts that
automatically resizes VMs in My Subscription.
I'll start by setting some parameters.
These determine if the script is going to scale VMs
up or down and what the VM sizes will be.
The next code logs into Azure using the Run As account.
Next, I'll set some variables
based on if we are scaling up or down.
Now, for the meat of the script,
this loops through the VMs in the subscription,
stops them if needed,
and then skills them up or
down depending on the chosen parameters.
Finally, this line starts the scaling loop. That's it.
Let's save the script and let's test it.
But let me first show you the VM in My Subscription.
I'll duplicate this tab and navigates to the VM. Here it is.
As you can see, the VMs size is Standard D2s. Let's remember that.
Now, to test it, we do that here in the "Test Pane".
I'll leave the parameters as they are.
This means that will scale down. Here we go.
The output of the script will show
here when I refresh the job stream.
First, it logs into Azure.
Next, it is scaling.
This shouldn't take long. Here we go.
Scaling is finished.
Let's take a look by going to the VM in
the other tab and refresh it.
Yes, it is now Standard B1ms.
It was Standard D2s. It worked.
Powerful stuff. As we've seen,
Azure Automation is super easy to use and can help you
to automate everything in Azure. Go check it out.