Tenant Manager Background

How to Use Azure Automation Runbooks for MSP Customers

Written by Hornetsecurity / 04.07.2019 /
Home » Blog » How to Use Azure Automation Runbooks for MSP Customers

Microsoft has made great strides in the hybrid cloud automation space with Azure Automation. For Managed Service Providers this is a great tool to take advantage of when managing multiple clients. We can now run our “scheduled tasks” on-premise with Azure Automation and get the following benefits:

  • Azure Log Monitoring – We can now configure alerts for scheduled tasks with Azure Monitor Logs. Now there is more visibility into our scheduled scripts that fail.
  • Encrypted Credentials – Azure Automation provides that ability to securely store credentials and call them via scripts. This one is huge, as we can easily call credentials without having to mess around with encrypted password files and certificates.
  • Hybrid Cloud Versatility with Scripts – We can choose whether to run a script either on-prem, in Azure, or both. This gives us more versatility to run our scripts anywhere.

Looking for general PowerShell Credential Encryption Guidance? See our guide on encrypting passwords with PowerShell! Looking for ways an MSP can get started with Azure? We have more resources to help MSPs get started with Azure!

Setting Up an Automation Account

  To get started using Azure Automation Runbooks, we need to have an Automation Account set up. If you don’t have an Azure account already, sign up for the free trial. Then login to the portal and search for Automation Accounts. Select the Add button to create a new account:

How to Use Azure Automation Runbooks for MSP Customers

Fill out the required fields to create the Automation Account. Select Yes to create the Azure Run As account, we could create it manually if needed but the easiest route it to just have Azure create it when setting up the Azure Automation account:

How to Use Azure Automation Runbooks for MSP Customers

How to Create an Azure Log Analytics Workspace

  In order to use Azure Automation Runbooks on-premise, we will need to set up a Log Analytics Workspace. This a service in Azure that provides monitoring and logging for the various Azure services. In the Azure Portal search for Log Analytics Workspaces, select Add:

How to Use Azure Automation Runbooks for MSP Customers

Fill out the required fields, the pricing for Log Analytics is based on storage, so your only paying for the storage to store your logs:

How to Use Azure Automation Runbooks for MSP Customers

Now we need to link our Azure Automation account with our Log Analytics Workspace. As of right now, this has to be done through PowerShell. So open up an administrative PowerShell window and run the following command to install the AZ module:

Install-Module AZ -Force

Then run Connect-AZAccount to connect to your Azure Subscription:

Connect-AZAccount

Now we need to get the resource ID for our Automation Account, we’ll use the Get-AZResource cmdlet and filter by resource type and our automation account name. In my example it’s LukeLabAA. We want to save the resource ID to a variable so we can use it shortly:

$AAResourceID = (Get-AzResource -ResourceType “Microsoft.Automation/automationAccounts” -Name “lukelabaa”).resourceid

We will do the same for the workspace resource ID using the same cmdlet with the workspace resource type and the name of the workspace we just set up:

$WSResourceID = (Get-AzResource -ResourceType “Microsoft.OperationalInsights/workspaces” -Name “lukelabaa-LA”).resourceid

To link the account with the workspace we will use the Set-AZDiagnosticSetting cmdlet and reference both resource ID’s:

Set-AzDiagnosticSetting -ResourceId $AAResourceID -WorkspaceId $WSResourceID -Enabled 1

To verify that the account is linked we can see in the output that JobLogs and JobStreams are enabled:

How to Use Azure Automation Runbooks for MSP Customers

Setting Up the Hybrid Worker Node

  The Hybrid Worker Node is an agent that is installed on an on-premise server running either Linux or Windows. This agent is used to execute commands from the runbook to the on-premise environment. The image below from Microsoft’s documentation gives a good depiction on the high-level topology for the communication between the Hybrid Runbook Worker and Azure Automation. You can group Runbook Workers together to create a redundant solution for your Runbooks. Also, note the port 443 connectivity which provides us with a secure way of transferring data back and forth between on-prem and cloud:

In this example, we’ll configure a Windows Server 2016 Core node with the Hybrid Worker Agent. Currently, as this article is being published, there are two ways to set this up, there is a PowerShell script that can be downloaded from the PowerShell gallery and ran, however, it is using the AzureRM cmdlets and running Connect-AzureRMAccount on server core produces the “Unable to load DLL ‘IEFRAME.dll’” error. So we go over how to add the Hybrid Worker Node on Server Core using the manual process. First, we will need to run the following command with the resource group and name of our log analytics workspace that we set up. This tells our workspace to push the worker components to the agent computer when we add it to the workspace in the next steps :

Set-AzureRmOperationalInsightsIntelligencePack -ResourceGroupName LukeLabAA-RG -WorkspaceName LukeLabAA-LA -IntelligencePackName “AzureAutomation” -Enabled $true

How to Use Azure Automation Runbooks for MSP Customers

Next, we will download the agent from our workspace. Navigate to the Log Analytics Workspace and select Advanced Settings on the left-hand side. Select Connected Sources and since we are setting up a Windows node we will choose Windows Servers. Select Download Windows Agent (64 bit) and transfer it to the Hybrid Worker. Also, make note of the Workspace ID and Primary Key, these need to be used in order to configure the agent installation to point to the Azure environment:

How to Use Azure Automation Runbooks for MSP Customers

When we run the executable click next through the wizard. Select Connect the agent to Azure Log Analytics (OMS) and click Next:

How to Use Azure Automation Runbooks for MSP Customers

Paste in the Workspace ID and Primary Key that we saw from the previous step, choose Azure Commercial and click Next, then Install:

How to Use Azure Automation Runbooks for MSP Customers

Wait a few minutes for the agent to show in the workspace

When the agent installs the Hybrid Registration PowerShell module gets copied down to the Hybrid Worker. So, on the Hybrid Worker node navigate to “C:\Program Files\Microsoft Monitoring Agent\Agent\AzureAutomation\<version>\HybridRegistration” and import the module:

Import-Module .\HybridRegistration.psd1

Then run the following command. The URL and Token are obtained from the Azure Automation account. Select Keys on the left-hand side and the Primary Key will be the token and the URL will be displayed. Also include the Hybrid Worker Group name that you would like to use if the one specified doesn’t exist it will automatically get created:

Add-HybridRunbookWorker –GroupName LukeLabOnPrem -EndPoint “https://eus2-agentservice-prod-1.azure-automation.net/accounts/d3d71ed2-e761-4333-b333-fce7b97e3333” -Token “0B/RNjlieKGSk2QjXmsuGoQtSQW0QVb6vfjqIY2342KJOiYOmedVP/vY+vpP8sfwdomliECn/GTasWmViJg==”

Now when we go to our Automation Account and select Hybrid Worker Groups we can see our new hybrid worker under the LukeLabOnPrem group we specified:

How to Use Azure Automation Runbooks for MSP Customers

How to Use an Azure Automation Runbook

Let test out running a Runbook through our new Hybrid Worker. I have installed the VMware PowerCLI module onto the node. We will run a simple script that will connect to our ESXi host and display a list of all the VMs. First, let’s add in some credentials. This is one of the coolest features of Azure Automation. Go to the automation account and select Credentials on the left-hand side. Choose the Add a Credential option and input some credentials, in this example I’m inputting my credentials to my VMware environment so we can use them in our runbook:

How to Use Azure Automation Runbooks for MSP Customers

Now, let’s create a Runbook. In the Automation Account select Runbooks on the left-hand side and choose Create a Runbook:

How to Use Azure Automation Runbooks for MSP Customers

Fill out the required fields, I am going to create a runbook called VMwareVMs, there can’t be spaces in the name:

How to Use Azure Automation Runbooks for MSP Customers

Another slick feature to point out, while we are creating our scripts in the runbook editor, we can select Assets on the left-hand side and choose our credentials that we saved and select Add to canvas. This will paste in the exact command that we need to retrieve those credentials:

How to Use Azure Automation Runbooks for MSP Customers

Now that I have my quick script to retrieve VM info, I’ll Save and Publish the runbook:

How to Use Azure Automation Runbooks for MSP Customers

When we go to Start the runbook, we have the option to have it run from our Hybrid Worker. I also selected the LukeLabOnPrem worker group:

How to Use Azure Automation Runbooks for MSP Customers
How to Use Azure Automation Runbooks for MSP Customers

The runbook will kick off on-premise and retrieve the VM information from the Get-VM PowerCLI cmdlet proving that our runbook is executing and connecting to infrastructure on-premise:

How to Use Azure Automation Runbooks for MSP Customers

Wrap-Up

  Managed Service Providers should definitely take advantage of the hybrid worker option with Azure Automation Runbooks. It can be a great tool to have in the back pocket for not only clients that have hybrid cloud solutions, but also MSP cloud solutions that require an on-premise presence into client environments. Instead of setting up scheduled tasks in native Windows Server where there is no centralized reporting or visibility on the status of a failed task, consider using Azure Runbooks with the power of Log Analytics alerting.

Thanks for reading!