Azure Automation – Running an on-premises PowerShell Script


So I thought it was about time to get stuck into Azure Automation. I am a fan of the product in theory, but have yet to delve into the details and figure it all out. Currently we run a number of on-premises powershell scripts on a number of servers performing various tasks. They get the job done, but in a decentralized way with limited visibility.

Having a product like Azure Automation storing, versioning, reporting, and of course, executing scripts would definitely add value, and it’s about time I get out of my old habits.

It’s not like me to start a blog straight into techie stuff, so here is my favourite joke…

I went to a Zoo and all they had was a dog.. it was a Shitzu

Ok that works better when spoken than written down… Now into it!

Setting  up Azure Automation

  1. First up log on to your Azure Portal
  2. Click Add –> Monitoring + Management –> Automation
  3. Then set up an Automation Account. It needs some basic stuff, that is a name, subscription, resource group, and location.
  4. Click Create!
Create the Automation Account


Create Operational Management Suite Workspace

If you don’t already have an Operational Management Suite account, create one as follows

  2. Complete the registration details to create an account
  3. Associate this with your Azure Active Directory

Deploy the Automation Solution in your OMS Workspace

  1. Log into your Operational Management Suite portal (Created above)
  2. Click Solutions –> Automation & Control to deploy the Automation Solution,
Add Automation and Control
  1. Click Add to then deploy the solution.
  2. Alternatively,  If a configure workspace button is only visible, click this first and associate it with your Resource Group and Automation Account created earlier. After which you can then Add the solution

Install Microsoft Monitoring Agent

  1. On an on premises machine you want to use as a Hybrid runbook worker, install the Microsoft Management Agent
  2. Login to your OMS Workspace
  3. Click on Settings –>Connected Sources–>Windows Servers
  4. Download the appropriate Windows Agent.

    Download 32 or 64 bit Agent. Take note of Workspace ID and Primary Key for later use
  5. Kick off the install of the agent on your designated server.
  6. Ensure you check the box “Connect the agent to Azure Log Analytics (OMS)”
  7. Enter the Workspace ID and Workspace Primary Key specified in your OMS portal (see above screenshot).
  8. Complete the installation.
  9. Go to Control Panel –> Microsoft Monitoring Agent
  10. Click on the Azure Log Analytics (OMS) tab and verify connection is succesful
    Verify agent has successfully connected to OMS


Create Hybrid Worker Group and connect agent to group

  1. Head back into your Azure portal
  2. Find your Automation Account created earlier (cloudy in my case)
  3. Click on the Keys Icon under essentials and take note of the Primary Access Key and URL
Azure Automation Account Keys
  1. Jump back on the server with the Monitoring Agent installed that you wish to use as your Hybrid Worker
  2. Start and Administrative Powershell Session and enter the following cmdlets to connect to Azure Automation

  3. Use the URL and Primary Access Key from your Automation Account
  4. For Group Name, enter any name you wish. This is the Hybrid Worker Group that will exist in your Azure Portal. If the group doesn’t exist it will be created.
  5. You should now be able to see the new group in the Azure portal under your Azure Automation Account!
Finally, the Hybrid Worker Group is created!
Finally, the Hybrid Worker Group is created!

Create a Powershell Script already!!!

  1. Now we are good to go! Jump back in your Azure Portal
  2. on the Automation Account Click Runbooks –> Add a runbook –> Create a new runbook
  3. Call it RestartWorker with the type set to PowerShell
  4. Click Create
The all powerful new script!
  1. Wait while it’s created, then once completed enter type the cmdlet:

  1. Click Save
  2. Click Test pane
  3. Select to run on a Hybrid Worker
  4. Select the Hybrid Worker group we created earlier
  5. Click Start
  6. All going to plan, your server has just restarted!
Runbook Results

And that’s it!

I know… thats an awful amount of work to restart a computer 🙂 But now we have the base down to do some more advanced scripting shenanigans! I hope this is of use to someone.