PowerShell: Create Azure Automation Account and Linked Log Analytics Workspace

I recently wrote about how to create an Azure Automation Account with an ARM template. This will be a quick follow up to show how to do the same, but using PowerShell, via the Azure Cloud Shell.

First of all, an automation account can be created using the following PowerShell one-liner:

New-AzureRmAutomationAccount -Name "MyAutomationAccount123" -Location "East US" -ResourceGroupName "ResourceGroup01" -Plan "Basic"

Next, we can create a new Log Analytics Workspace using:

New-AzOperationalInsightsWorkspace -Location "eastus" -Name "myloganalyticswkspace12" -Sku Standard -ResourceGroupName "ResourceGroup01"

Easy! We can put these commands together into the following simple script:

$ResourceGroup = "ResourceGroup01"
$WorkspaceName = "log-analytics-" + (Get-Random -Maximum 99999) 
$AutomationaccountName = "my-automation-account-" + (Get-Random -Maximum 99999) 
$Location = "eastus"
New-AzureRmAutomationAccount -Name $AutomationaccountName -Location $location -ResourceGroupName $ResourceGroup -Plan "Basic"
New-AzOperationalInsightsWorkspace -Location $Location -Name $WorkspaceName -Sku Standard -ResourceGroupName $ResourceGroup

Unfortunately, I haven’t seen a way to link the workspace to the automation account using PowerShell. This could be done using ARM template, however I haven’t yet found a way using PowerShell. Instead, this can be done manually in the Azure Portal by going to the new Log Analytics Workspace and then selecting Linked WorkSpace:

Thanks for reading! This article has covered how to deploy a Azure Automation Account and Log Analytics Workspace using PowerShell.

Related posts

Docker Exec Command With Practical Examples

Debugging with Git Bisect

A Beginners Guide to Azure Repos

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More