Get Started with Azure Automation
September 19, 2016Microsoft Azure has a neat way to store and run code right from within Microsoft Azure called “Azure Automation”. If you’re familiar with Amazon’s Lambda service, Azure Automation is similar in many ways. The main difference is that in Azure, we’re working with PowerShell code instead of Python or Node.js.
Create An Azure Automation Account
To get started, the first thing that we need to do is to setup an Azure Automation Account. In your Azure instance, browse for “Automation Accounts” and then click Add. Give the account a name and a subscription that the PowerShell commands should run under. As with any Azure objects, select a resource group or create your own and then select a location. The last setting is to decide whether or not the account with be an “Azure Run As” account. If you select “Yes” then the account will have access to other Azure Resources within your instance. For our examples, this account should be a “run as” account.
Building a Runbook
Now that the account has been created, we can open the account properties and go runbooks. A runbook is the script that will be executed through the Azure portal. Click Create a new runbook (you can import one if you already have existing runbooks as well) and give the runbook a name. Then under the “Runbook type” field, you’ll need to select the type of runbook. There are two main types of runbooks, PowerShell or Graphical. Powershell workflows allow you to type PowerShell code directly into the Azure window, while the Graphical type will show you cmdlets and they can be added by clicking on them. For our purposes we’ll be typing the PowerShell code in directly, so we won’t use the Graphical type. Add a description and click the “Create” button.
Now we can edit our runbook and put our PowerShell code into the work pane. I’ve added some code here to authenticate with Microsoft Azure and get a list of all virtual machines with “test” somewhere in their name. Then stop the virtual machine. Maybe you even have the code delete virtual machines with “test” in their name to cleanup your Azure subscriptions and stop paying for those resources. You can make the code look how you want it.
Once you’ve written your PowerShell code, you can click the “Test pane” link to take you to the test window. Here you can enter any parameters that might be involved in the script and execute it. The status will show up as queued in the test pane and then eventually will run. This usually isn’t immediately executed, but should execute fairly quickly after being submitted. The code will execute and show you any results in the pane.
Once you’ve tested your code and are happy with it, you can “Publish” the code to make it available to your users. The Runbooks screen will show you the current status.
Create an Entry Point
Now that the code is published you can run it from the Azure portal, but you can also set it on a schedule, or create a webhook to call through a REST call. In our example a schedule might be a good way to execute the code and shutdown your test VMs on a specific routine to cut costs, but here I’m actually going to add a Webhook so that I can execute it through a REST call.
I click Add Webhook and create a new one. The webhook also gets a name and expiration date as well as being able to enable or disable it from the Internet. The last box will show the URL used to make a POST request. Be sure to copy that URL to use it later on. You can also set some of the configuration parameters ahead of time so that you don’t pass them through the REST call, its up to you.
Execute the Code
I used an online REST call maker and pasted in my Request URL. This triggers the PowerShell code we just created through the webhook.
Once I run it, a job will be returned, and we can view the job status on the runbook screen.
When I look in my virtual machines screen, I see that my virtual machine named “testvm” is now stopped thanks to our script running.
Additional Options
Credentials
Under your Automation Account, in Assets you can also add credentials so that they can be used in future scripts. Perhaps you need credentials to login to some of your virtual machines to execute PowerShell scripts on them. The credentials screen lets you store these in the portal for reuse.
Install PowerShell Modules
In some cases you may want to add additional PowerShell functionality to your Azure instance. For instance, you might want to install the Posh-SSH module so that you can execute ssh commands from PowerShell. You can do this in a couple of ways, the first is to go to your Azure Automation account and go to the assets option. Select Modules and then you can browse the gallery for the module you want to install in your Azure instance.
The second way is to go right to the PowerShell Gallery and find the module. Microsof thas added a very nice “Deploy to Azure Automation” button to add the module to your Azure instance.zure
Great explanation.
I am new be to Azure PowerShell scripting. Here I am having a requirement like we want to delete old(more than 90days) files from Data Lake store. After lot of research I got to know PowerShell script will help out on this and I tested many scripts none of helped me. So could you please help me on this.
Thanks,
Shekar