Azure Automation PowerShell Runbook

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.AZAU2

Give the Automation account a name, assign to a subscription, resource group and location.  Leave create as yes and click crate. 

AZAU3

Once the task has completed there will now be Automation account showing. AZAU10

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.AZAU4

Once in runbooks there will be some pre-configure runbooks that can be used as references. 

To create a new runbook click Add a runbookAZAU11

Once in the runbook give a name and select the runbook type in this case it will be PowerShell. Then click create. AZAU5

Click on edit to modify the PowerShell script. AZAU6

The connection commands is generic and is copied from AzureAutomationTutorialScript runbook.

$connectionName = “AzureRunAsConnection”
try
{
# Get the connection “AzureRunAsConnection “
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName

 

“Logging in to Azure…”
Add-AzureRmAccount -ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = “Connection $connectionName not found.”
throw$ErrorMessage
} else{
Write-Error -Message $_.Exception
throw$_.Exception
}
}
 

I have added the command I want to run at the bottom. Once finished, the runbook needs to be saved and published. 

AZAU7

To test if the script will work as expected there is a test pane icon 
AZAU12
 
Once in test click start and the script will be executed the output of the command will be returned to the console. 
AZAU13
 
Once the runbook is saved, the start icon will become available so the runbook can be executed. 
AZAU8
 
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. 
 
AZAU14
 
Once the schedule has been created it will show under schedules. 
AZAU15
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s