In this post I am going to go through setting up an Azure automation account and creating a runbook to PowerShell.
Azure Automation allows for process automation, configuration management, update management and PowerShell script execution on both Azure / Office 365. I like using Azure Automation as it allows me to save credentials in Azure for running task opposed to having them called in my script which is less secure.
I am going to setup a runbook to power on and off my LAB servers so that they only run during the day.
To start using Azure Automation
Go to All services > Automation Accounts
Once in Automation Accounts we need to create a new account.
Give the Automation account a name, assign to a subscription, resource group and location. Leave create as yes and click crate.
Once the task has completed there will now be Automation account showing.
Next step is to create a runbook to run the PowerShell command. Click on the Automation account and go to process automation and click on runbooks.
Once in runbooks there will be some pre-configure runbooks that can be used as references.
To create a new runbook click Add a runbook
Once in the runbook give a name and select the runbook type in this case it will be PowerShell. Then click create.
Click on edit to modify the PowerShell script.
The connection commands is generic and is copied from AzureAutomationTutorialScript runbook.
$connectionName = “AzureRunAsConnection”
# Get the connection “AzureRunAsConnection “
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
“Logging in to Azure…”
Add-AzureRmAccount -ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
$ErrorMessage = “Connection $connectionName not found.”
Write-Error -Message $_.Exception
I have added the command I want to run at the bottom. Once finished, the runbook needs to be saved and published.
To test if the script will work as expected there is a test pane icon
Once in test click start and the script will be executed the output of the command will be returned to the console.
Once the runbook is saved, the start icon will become available so the runbook can be executed.
Last step is to schedule when the runbook will execute, click schedule.
Add a name, description, start time/date, time zone and if the task is a once of or a recurring task.
Once the schedule has been created it will show under schedules.