Or as we say in the north of Germany: "Moin Moin!"
I’m a Microsoft Senior Cloud Solution Architect – Engineering (or short Sr. CSA-E) and in this article I want to talk about how to automate the hybrid world.
Over the years Microsoft has developed more and more automation tools such as Power Automate, Azure Logic Apps, Azure Functions or Azure Automation. While Azure Automation is not the latest tool in the automation tools family, I dedicate this blog this amazing tool and hopefully provide you with an easy-to-follow how-to guide.
Set rights for run as account
Create and run a Runbook
A more complex runbook
Install required Cmdlets
Create Automation Variables
Add the Runbook-Script
Start the Runbook using the Azure portal
Start the Runbook using a webhook
Start a Runbook using a schedule
Monitoring and Logging
Secure access to Azure Accounts
This is part two of a two part series. If you haven't read the first part yet, I recommend you read part one first and come back to this one since it builds on what I’ve done in part one.
You will find part one in the list of my other articles here: https://aka.ms/JonasOhmsenBlogs
Now let's finish setting up the Hybrid Worker and start playing with the Runbook feature of Azure Automation.
So, grab a coffee or two and follow along if you like.
You could skip this section if you chose not to use a "run as account" for the Hybrid Worker group. (Described in part one of this two part article)
Since we want to make sure the service account has least privileges possible, let’s configure the following settings:
We can either use a GPO to give the user appropriate permissions or the local group policy editor for testing.
It is now time to create and run our first Runbook on the new Hybrid Worker.
Let’s create a quite simple Runbook and validate the output, before we start a more complex approach.
Get-date -f u
$Env:COMPUTERNAME
Since everything is set up and working, we can now use a more complex runbook and really automate something.
In this section we will do the following:
In the case of ConfigMgr we need to install the ConfigMgr console on the Hybrid Worker server. That gives us access to the ConfigMgr cmdlets. Please refer to the ConfigMgr documentation on how to do so. Or use any other Cmdlet you want to test with. (The example script expects the ConfigMgr cmdlet)
For the Example-Runbook to work we need four variables.
Variable Name | Value Description |
Var-StartString1 |
Create a GUID by running “New-Guid” in PowerShell or use any password like string. The string is used to validate if the runbook is allowed to run or not. I will further explain this method when we start the runbook.
|
Var-StartString2 |
Like Var-StartString1 but with a different value
|
Var-ProviderName |
The FQDN of the ConfigMgr SMS Provider.
|
Var-SiteCode |
The ConfigMgr site code. Example: “P01”
|
Use the list above to create four variables
Azure Portal - Automation Account List of Variables
Download or copy the example runbook from here: ConfigMgr-RunbookExample1.ps1 and create a new runbook as we did earlier. (Described in Create and run a Runbook)
The script is designed to run in Azure automation using any of the available start methods, in PowerShell ISE or in Visual Studio Code for example. This makes testing really easy and gives you enough examples to create more complex Runbooks.
The example script contains comments to explain the inner workings, but it basically does the following: Script Process Flow Diagram
If we start our new Runbook, as we did before with the simple one, we will be asked for parameter values this time.
Since we’re not using a webhook (a webhook will be explained in the next step) and we read “ProviderMachineName” and “SiteCode” from Azure Automation variables, we only need to fill four variables as illustrated in the screenshot.
As “STARTSTRING” use one of the values of Var-StartString1 or Var-StartString2. The two strings act as a password-like string to prevent anyone from starting the runbook not having the correct start string. (It might make more sense in the next example)
As “SYSTEMNAME” use anything you like.
As “SYSTEMMACADDRESS” use a valid Mac address
As “COLLECTIONNAME” us a valid ConfigMgr collection-name Make sure to choose the Hybrid Worker Group down below and click “OK” to start runbook execution.
We will then be redirected to the “Job Status Page” to see the result of the execution. |
A webhook is an URL you can use to directly start a runbook without the need to click on “Run” in the Azure Portal.
The Azure Portal treats each webhook as a password and will only show the full URL once. That’s because it is kind of having a password to start the runbook and we don’t need any extra authentication method. That is also the reason why we use the parameter “StartString” and the two “RunVariables” to have some kind of extra validation layer before we let the runbook fully run its actions.
NOTE: Always treat the URL as if it were a password and store it safely.
Choose “Create new webhook” (1)
We can now post data to the webhook URL to start our Runbook from anywhere and from any application that supports the post method.
Use the following example script to start the runbook using the webhook via PowerShell for example.
# URL of the webhook
$webhookURL = 'https://5abf541d-717f-4ef9-8df6-5e985d6d0ddf.webhook.ne.azure-automation.net/webhooks?token=[tokenvalue]'
# A header
$header = @{message='We need a new machine'}
# The body contains the neccesary parameter values
$body = @{
SystemName='TestSystem005'
SystemMacAdress='00:11:22:33:44:AA'
CollectionName = 'All Systems'
StartString = '64fe693f-150e-4593-a1e1-6cb0f3f11114'
} | ConvertTo-Json # we need to convert the body to JSON
# A simple POST invokes the runbook
Invoke-RestMethod -Method Post -Uri $webhookURL -Headers $header -Body $body
In the case of the example runbook a schedule does not make much sense, because we use it to import computer information into ConfigMgr and we have changing parameter values.
But it’s important to know that we could also create a schedule to run the runbook periodically.
To view runbook states we have multiple options. We used some of them already.
It is also possible to export Azure Automation related events to a Log Analytics Workspace to have more reporting capabilities and the ability to use “Azure Workbooks”.
Go to your Azure Automation account and click on “Diagnostic Settings” and “+ Add diagnostic setting” to configure the export if you like.
There are multiple built-in roles for Azure Automation and Arc connected machines such as:
For Azure Arc connected machines:
Use the built-in roles or create custom ones to restrict access to the Automation Account and Arc enabled servers.
I hope you had fun following along to automate the hybrid world.
Keep in mind that you can do a lot more with an Azure Arc enabled server as just running runbooks on a Hybrid Worker.
Azure Automation and Azure Arc are amazing tools, and both can help you automate and simplify server-, process- and service-management. Have fun with them!
Stay safe!
Jonas Ohmsen
Microsoft Germany
Disclaimer:
This posting is provided "AS IS" with no warranties and confers no rights.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.