I recently looking for Azure Automation, from top to bottom. It’s why, in the next 2 articles, we will see how to use this tool, from A to Z:
- [Azure Automation] Interface discovery – Part 1
- [Azure Automation] Migrate your scripts to Azure – Part 2 (this post)
Today, we will see how to migrate your On-Premises scripts, to Azure Automation. In my environment, I had 5 PowerShell scripts that ran with Schedule Tasks:
- BackupOneDriveFolder: Backup my OneDrive folder to my Synology & on an Azure Blob Storage
- BackuppfSenseConfiguration: Backup the configuration of my pfSense to Azure Blob Storage
- CheckCertificateValidity: Check the validity of certificates of my websites
- GetHyperVReport: Generate a report of my Hyper-V Server
- UpdateS2SPublicIP: Update the public IP of my gateway, based on my NoIP
The principle is simple: migrate these 5 scripts to Azure Automation. I’ll need to use Azure and Hybrid Worker to run these scripts. The first step is to create your Schedules, based on what you are using with Tasks Scheduler. On your Azure Automation account, go to Schedules and create the schedule that you need:
The next step is to import module that you need to run your scripts, in the Modules part. If you use Hybrid Worker, install the right modules in the server that you will use for Hybrid connection:
In the Assets part, create Variables that you will use in your scripts. Create Credentials too:
When these prerequisites are ready, we can start the migration.
First, create a new Runbook, give it a name and choose PowerShell as type. Paste the PowerShell script associated with your script:
Adapt variables that use On-Prem a Username/Password or a key with the variable that you created or the credentials. You will use Get-AutomationVariable to get Variable and Get-AutomationPSCredential to get credentials. Normally, it’s the only part that you need to change. The script stays the same. You can test your script by using the Test Pane. And because I need to copy files to my Synology who is located On-Prem, I must use a Hybrid Worker:
When the script is finished, you should see this in the logs part:
Now that I know that the script is working correctly, just Save and Publish the script. You can link a schedule to this runbook, based on the schedule created before. Don’t forget to modify parameters by choosing if you want to run the script On-Prem with Hybrid Worker or on Azure. If your script is asking for parameters, you can complete them here:
Do this for each script that you want to migrate. You can monitor your jobs that ran directly on the Jobs part of your Azure Automation account:
Like in your PowerShell session, if something went wrong, you’ve logs:
This process is very simple. After you migrated all of your scripts, you have only one console to manage scripts, logs, variables, etc… So, don’t hesitate to migrate 🙂