Azure Automation – Running scripts locally on VM through runbooks

I was tasked to create a powershell script to run on a schedule on a Azure VM. Normally this would be running as a scheduled task on the VM but seeing as we’re working with AzureVM and schedule tasks are legacy I wanted to explore the possibilities of running the schedule and script in Azure to keep the VM clean and the configuration scalable.

After some research the best option would be running the powershell script as a CustomScriptExtension on the VM, and the schedule would be handled by a Process Automation Runbook (using Automation Accounts).

What I ended up with is the script below. It’s fairly easy to configure and contains almost all the required configuration in the parameters.

How does it work? Simple!


  1. Create a Storage Account
    • Create a Private Blob container
  2. Create a Automation Account
    • Make sure a RunAsAccount is created

Runbook configuration

  1. Navigate to the Automation Account you intend to use
  2. Create a Powershell runbook and press Edit
  3. Copy the below script into the runbook and save
    • Fill out the parameters with relevant information (subscription, resourcegroup etc)
    • Make sure to set the name of the StorageContainer to the one where you want to host the scripts
    • Extension name should be unique for the job, as well as the ScriptName
    • The scriptblock parameter takes the script you intend to run on the VMs
  4. On the first run, go into the Test Pane from edit view and edit the UploadScript parameter to $True
    • This will make the runbook actually save the script to the Container, allowing the VM download and run the script
  5. Done!

Now simply register schedules to the runbook. If you want to run the script on several VM you have two options. Either specify multiple VM when creating schedules or create a schedule per machine you want to run the script against.

Disclaimer: All information on this blog is offered "as is" with no warranty. It is strongly recommended that you verify all information and validate all scripts in isolated test environments before using them in production environments.