top of page
11 minutes ago
22 min read

Azure Workshopz | Beyond Cloud with Chriz

DEPLOY • AUTOMATE • BREAK • DIAGNOSE • REPAIR • VERIFY • CLEAN UP

Level: Intermediate to AdvancedPlatform: Microsoft AzureTools: Azure CLI, Azure Cloud Shell (Bash), Windows PowerShell 5.1Deployment: Azure CLI only — no Terraform, Bicep, or Azure Portal configuration requiredTechnologies: Azure Automation, Hybrid Runbook Worker, Azure Virtual Machines, Managed Identities, Azure RBAC, Runbooks, Azure MonitorCost level: Medium

Introduction

Your Azure virtual machines are running, your automation scripts are ready, and your environment looks healthy.

But what happens when an Azure Automation runbook suddenly stops working?

What if the Hybrid Runbook Worker is online but its managed identity cannot access an Azure resource?

What happens when permissions are removed, the worker service stops, or a job fails during execution?

And how do you determine whether the problem is related to Azure RBAC, managed identity authentication, the Hybrid Worker extension, or the runbook itself?

In this workshop, we're going to build a complete Azure Automation Hybrid Runbook Worker environment.

We will deploy an Azure Automation Account, configure managed identities, create a Windows Server 2022 virtual machine, install an extension-based Hybrid Runbook Worker, and register it with a Hybrid Worker Group.

Then we'll create a PowerShell runbook that performs a local operation on the worker and retrieves Azure resource information using managed identity authentication.

Once the automation works, we'll deliberately break it.

We'll remove the Automation Account identity's permissions, trigger failed executions, investigate the error messages, restore authorization, and verify successful job execution.

Finally, we'll simulate a worker service outage and recover the automation infrastructure.

All Azure resource operations will be performed through Azure CLI.

The goal is to understand how Azure Automation behaves during real operational incidents — not simply how to run a script.

What You'll Learn

By the end of this workshop, you will be able to:

  1. Deploy an Azure Automation Account.

  2. Configure a system-assigned managed identity for the Automation Account.

  3. Deploy a Windows Server VM to host a Hybrid Runbook Worker.

  4. Configure the VM's system-assigned managed identity.

  5. Create a Hybrid Runbook Worker Group.

  6. Register an Azure VM as a Hybrid Runbook Worker.

  7. Install the extension-based Hybrid Runbook Worker.

  8. Configure the PowerShell execution environment.

  9. Create and publish Azure Automation runbooks using Azure CLI.

  10. Execute runbooks on a specific Hybrid Worker Group.

  11. Retrieve and investigate job execution logs.

  12. Configure least-privilege RBAC permissions.

  13. Simulate a managed identity authorization failure.

  14. Diagnose failed runbook executions.

  15. Restore permissions and recover automation.

  16. Simulate a Hybrid Worker service outage.

  17. Validate the complete environment and clean up resources.

Workshop Architecture

We will deploy one Automation Account and one Windows Server virtual machine.

The Windows VM acts as the Hybrid Runbook Worker.

                    AZURE CLOUD SHELL
                           |
                        Azure CLI
                           |
                           v
                 +---------------------+
                 |  AZURE AUTOMATION   |
                 |       ACCOUNT       |
                 |                     |
                 | System-Assigned MI  |
                 |                     |
                 | PowerShell Runbooks |
                 | Jobs / Job History  |
                 +----------+----------+
                            |
                            | Run On
                            v
                 +---------------------+
                 |   HYBRID WORKER     |
                 |       GROUP         |
                 +----------+----------+
                            |
                            v
             +----------------------------+
             |      WINDOWS SERVER 2022   |
             |                            |
             | Hybrid Worker Extension    |
             | PowerShell 5.1             |
             | Az.Accounts / Az.Resources |
             | VM Managed Identity        |
             |                            |
             | Local File Operations      |
             | Azure Resource Queries     |
             +-------------+--------------+
                           |
                           | Managed Identity
                           v
                +-----------------------+
                |       AZURE RBAC      |
                |                       |
                | Reader Role           |
                | Resource Group Scope  |
                +-----------+-----------+
                            |
                            v
                  Azure VM Resource

The two managed identities

This architecture uses two separate identities.

Identity

Purpose

VM system-assigned identity

Used by the Hybrid Worker extension for worker registration and service communication

Automation Account system-assigned identity

Used by our runbook to authenticate to Azure resources

Important: When the Automation Account has its own system-assigned managed identity, that account-level identity is used by hybrid runbooks for Azure authentication.

Assigning Reader to the VM identity instead of the Automation Account identity will not solve the authorization problem in this workshop.

This distinction is essential to the failure scenario.

Workshop Failure Scenarios

We will perform two incidents.

Incident

What we break

Expected impact

Incident 1

Remove the Automation Account identity's Reader role

Runbook fails when querying Azure

Incident 2

Stop the Hybrid Worker service

Worker stops processing automation jobs

In each case, we will inspect the relevant job or worker state and restore operation.

Our expected workflow is:

DEPLOY → AUTOMATE → BREAK → DIAGNOSE → REPAIR → VERIFY

Prerequisites & Cost Warning

Requirements

You need:

  • An active Azure subscription.

  • Azure CLI version 2.75.0 or later.

  • Azure Cloud Shell with Bash.

  • Permission to create Azure Automation resources.

  • Permission to create Azure VMs and networking resources.

  • Permission to create and delete Azure role assignments.

  • Sufficient compute quota for a Windows Server VM.

  • Outbound HTTPS connectivity from the worker.

  • A region that supports Azure Automation and the VM SKU.

The workshop uses the automation Azure CLI extension.

Cost considerations

Azure Automation offers included job-processing allowances depending on current pricing and subscription conditions. Additional execution can be billed.

Our main costs will come from the Windows Server VM and its supporting infrastructure.

Resource

Cost consideration

Azure Automation

Job processing beyond applicable allowances

Windows Server VM

Compute and Windows licensing

Managed OS disk

Storage

Standard Public IP

Public IP resource

Virtual Network and NSG

Generally no fixed deployment charge

Azure Monitor

Applicable monitoring usage

We'll use a small Windows VM and delete everything when finished.

The public IP is used only to provide an explicit outbound internet path for the lab VM. No inbound RDP rule will be opened.

For production, use private connectivity and controlled outbound access through an appropriate network design.

Pricing:

Lab 1 — Prepare Azure Cloud Shell

Step 1 — Open Azure Cloud Shell

Open:

Select Bash.

Verify Azure CLI:

az version

Check the active subscription:

az account show \
  --query "{Name:name,SubscriptionId:id,TenantId:tenantId}" \
  --output table

If necessary, select the subscription:

az account set \
  --subscription "<YOUR-SUBSCRIPTION-ID>"

Step 2 — Install the Automation extension

az extension add \
  --name automation \
  --upgrade

Verify installation:

az extension show \
  --name automation \
  --query "{Name:name,Version:version}" \
  --output table

Inspect the available commands:

az automation --help
az automation hrwg --help
az automation runbook --help

Important: Some Azure Automation CLI commands are currently classified as experimental. The workshop uses Azure CLI and documented REST endpoints, but CLI syntax may evolve between extension versions.

Lab 2 — Configure Workshop Variables

We'll use environment variables to keep resource names consistent.

Run the following commands in the same Bash session.

# General configuration
export LOCATION="westeurope"
export SUFFIX=$(openssl rand -hex 3)

# Azure subscription
export SUB_ID=$(az account show --query id -o tsv)

# Resource Group
export RG="rg-automation-deepdive-$SUFFIX"

# Azure Automation
export AUTOMATION="aa-hybrid-$SUFFIX"
export WORKER_GROUP="hrwg-operations"
export RUNBOOK="rb-hybrid-resource-check"

# Windows VM
export VM="vm-hybrid-$SUFFIX"
export VM_SIZE="Standard_D2s_v5"

# Networking
export VNET="vnet-automation-$SUFFIX"
export SUBNET="snet-workers"
export NSG="nsg-hybrid-$SUFFIX"
export PIP="pip-hybrid-$SUFFIX"

# Hybrid Worker
export WORKER_ID=$(cat /proc/sys/kernel/random/uuid)
export EXTENSION_NAME="HybridWorkerExtension"

# REST API
export API_VERSION="2024-10-23"

Display the configuration:

echo "Resource Group: $RG"
echo "Automation:     $AUTOMATION"
echo "Worker Group:   $WORKER_GROUP"
echo "VM:             $VM"
echo "Region:         $LOCATION"

Step 2 — Create the resource group

az group create \
  --name "$RG" \
  --location "$LOCATION" \
  --tags Environment=Workshop \
         Project=Azure-Automation-DeepDive

Step 3 — Retrieve its resource ID

export RG_ID=$(az group show \
  --name "$RG" \
  --query id \
  --output tsv)

echo "$RG_ID"

Lab 3 — Deploy the Worker Network

We will create a dedicated VNet for the worker.

Step 1 — Create the VNet

az network vnet create \
  --resource-group "$RG" \
  --name "$VNET" \
  --location "$LOCATION" \
  --address-prefixes 10.50.0.0/16 \
  --subnet-name "$SUBNET" \
  --subnet-prefixes 10.50.1.0/24

Step 2 — Create a Network Security Group

az network nsg create \
  --resource-group "$RG" \
  --name "$NSG" \
  --location "$LOCATION"

We deliberately do not create inbound RDP rules.

The default NSG configuration denies unsolicited inbound connections from the internet.

Step 3 — Create a Standard Public IP

az network public-ip create \
  --resource-group "$RG" \
  --name "$PIP" \
  --location "$LOCATION" \
  --sku Standard \
  --allocation-method Static

This provides explicit outbound connectivity for package installation and Azure service communication.

In a production environment, NAT Gateway or another managed outbound design would often be preferable.

Lab 4 — Deploy Windows Server 2022

The VM will host our Hybrid Runbook Worker.

Step 1 — Generate a temporary administrator password

export VM_PASSWORD="AzLab@$(openssl rand -hex 14)X9"

This is a synthetic workshop password.

Never commit it to a repository or reuse it in production.

Step 2 — Create the VM

az vm create \
  --resource-group "$RG" \
  --name "$VM" \
  --location "$LOCATION" \
  --image Win2022Datacenter \
  --size "$VM_SIZE" \
  --admin-username azureadmin \
  --admin-password "$VM_PASSWORD" \
  --vnet-name "$VNET" \
  --subnet "$SUBNET" \
  --nsg "$NSG" \
  --nsg-rule NONE \
  --public-ip-address "$PIP" \
  --public-ip-sku Standard \
  --assign-identity

The --assign-identity parameter enables a system-assigned managed identity for the VM.

Step 3 — Clear the temporary password

unset VM_PASSWORD

Step 4 — Verify VM deployment

az vm show \
  --resource-group "$RG" \
  --name "$VM" \
  --query "{Name:name,State:provisioningState,Size:hardwareProfile.vmSize}" \
  --output table

Step 5 — Verify the VM identity

az vm identity show \
  --resource-group "$RG" \
  --name "$VM" \
  --output json

Record the VM principal ID:

export VM_PRINCIPAL_ID=$(az vm identity show \
  --resource-group "$RG" \
  --name "$VM" \
  --query principalId \
  --output tsv)

Step 6 — Retrieve the VM resource ID

export VM_ID=$(az vm show \
  --resource-group "$RG" \
  --name "$VM" \
  --query id \
  --output tsv)

echo "$VM_ID"

Checkpoint: The VM must have a system-assigned identity and successfully provisioned VM agent.

Lab 5 — Create the Automation Account

Now we'll deploy the Azure Automation Account.

Step 1 — Create the account

az automation account create \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --location "$LOCATION" \
  --sku Basic

Step 2 — Verify the account

az automation account show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --output json

Step 3 — Retrieve its resource ID

export AUTOMATION_ID=$(az automation account show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --query id \
  --output tsv)

echo "$AUTOMATION_ID"

Step 4 — Enable the Automation Account managed identity

The Automation CLI account creation command does not expose the complete managed identity configuration in every extension version.

We'll use Azure CLI's az rest command to configure the identity through Azure Resource Manager.

az rest \
  --method patch \
  --uri "https://management.azure.com${AUTOMATION_ID}?api-version=${API_VERSION}" \
  --headers "Content-Type=application/json" \
  --body '{"identity":{"type":"SystemAssigned"}}'

Step 5 — Verify identity assignment

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}?api-version=${API_VERSION}" \
  --query identity \
  --output json

Expected: type is SystemAssigned, and principalId is populated.

Step 6 — Retrieve the Automation Account principal ID

export AUTOMATION_PRINCIPAL_ID=$(az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}?api-version=${API_VERSION}" \
  --query "identity.principalId" \
  --output tsv)

Display both identity IDs:

echo "VM identity:         $VM_PRINCIPAL_ID"
echo "Automation identity: $AUTOMATION_PRINCIPAL_ID"

The two IDs should be different.

This distinction is central to the workshop.

Lab 6 — Configure Managed Identity Permissions

Our runbook will read Azure resource information.

It does not need Contributor or Owner.

We'll assign the Automation Account identity the built-in Reader role at the workshop resource group scope.

Step 1 — Create the role assignment

export READER_ASSIGNMENT_ID=$(az role assignment create \
  --assignee-object-id "$AUTOMATION_PRINCIPAL_ID" \
  --assignee-principal-type ServicePrincipal \
  --role "Reader" \
  --scope "$RG_ID" \
  --query id \
  --output tsv)

Step 2 — Inspect the assignment

az role assignment list \
  --assignee "$AUTOMATION_PRINCIPAL_ID" \
  --scope "$RG_ID" \
  --output table

Expected:

Role:  Reader
Scope: Workshop Resource Group

Why Reader?

Our runbook only needs to retrieve Azure resource information.

The Reader role allows viewing resource metadata without granting modification rights.

For production, scope the role to the specific resources required by each runbook rather than assigning broad resource group permissions unnecessarily.

Lab 7 — Create a Hybrid Runbook Worker Group

Hybrid Runbook Workers are organized into groups.

A group can contain one or several workers.

Jobs are scheduled to a worker group rather than an individually selected VM.

Step 1 — Create the group

az automation hrwg create \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --name "$WORKER_GROUP"

Step 2 — Verify the group

az automation hrwg show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --name "$WORKER_GROUP" \
  --output json

Step 3 — Register the VM as a Hybrid Worker

az automation hrwg hrw create \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --hybrid-runbook-worker-group-name "$WORKER_GROUP" \
  --hybrid-runbook-worker-id "$WORKER_ID" \
  --vm-resource-id "$VM_ID"

Step 4 — List registered workers

az automation hrwg hrw list \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --hybrid-runbook-worker-group-name "$WORKER_GROUP" \
  --output json

The worker registration should now reference the Windows VM.

Important: Registration alone does not install or activate the extension.

We still need to install the Hybrid Runbook Worker extension.

Lab 8 — Install the Hybrid Runbook Worker Extension

We'll use the supported extension-based Hybrid Runbook Worker.

The older agent-based Hybrid Worker is retired and should not be used for new deployments.

Step 1 — Retrieve the Automation Hybrid Service URL

The extension needs the Automation Account's Hybrid Service URL.

export HYBRID_URL=$(az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}?api-version=${API_VERSION}" \
  --query "properties.automationHybridServiceUrl" \
  --output tsv)

Verify:

echo "$HYBRID_URL"

The URL must not be empty.

Step 2 — Prepare extension settings

cat > hybrid-settings.json <<EOF
{
  "AutomationAccountURL": "$HYBRID_URL"
}
EOF

Step 3 — Install the extension

az vm extension set \
  --resource-group "$RG" \
  --vm-name "$VM" \
  --publisher Microsoft.Azure.Automation.HybridWorker \
  --name HybridWorkerForWindows \
  --extension-instance-name "$EXTENSION_NAME" \
  --version 1.1 \
  --settings @hybrid-settings.json \
  --enable-auto-upgrade true

Step 4 — Verify extension provisioning

az vm extension show \
  --resource-group "$RG" \
  --vm-name "$VM" \
  --name "$EXTENSION_NAME" \
  --instance-view \
  --output json

Expected provisioning state:

Succeeded

Step 5 — Verify the worker registration

az automation hrwg hrw show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --hybrid-runbook-worker-group-name "$WORKER_GROUP" \
  --hybrid-runbook-worker-id "$WORKER_ID" \
  --output json

Check the worker's reported state and heartbeat information.

An installed extension does not guarantee that the worker is successfully processing jobs.

We'll verify execution in the next labs.

Lab 9 — Prepare the PowerShell Execution Environment

Our Windows Hybrid Worker will execute PowerShell runbooks.

We'll install the Az modules needed by the runbook on the worker VM.

Important: Modules available in the Automation Account's cloud runtime are not automatically installed on a Hybrid Runbook Worker VM. The worker requires its own local modules.

Step 1 — Create the installation script

In Azure Cloud Shell, create:

cat > worker-setup.ps1 <<'POWERSHELL'
$ErrorActionPreference = "Stop"

[Net.ServicePointManager]::SecurityProtocol = `
    [Net.SecurityProtocolType]::Tls12

Write-Output "Preparing Hybrid Worker..."

Install-PackageProvider `
    -Name NuGet `
    -MinimumVersion 2.8.5.201 `
    -Force

Set-PSRepository `
    -Name PSGallery `
    -InstallationPolicy Trusted

Install-Module `
    -Name Az.Accounts,Az.Resources `
    -Scope AllUsers `
    -Force `
    -AllowClobber

New-Item `
    -Path "C:\ProgramData\AzureWorkshopz" `
    -ItemType Directory `
    -Force | Out-Null

Get-Module -ListAvailable `
    Az.Accounts,Az.Resources |
    Select-Object Name,Version,Path

Write-Output "Worker prerequisites installed."
POWERSHELL

Step 2 — Execute it through Azure Run Command

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts "$(cat worker-setup.ps1)" \
  --query "value[0].message" \
  --output tsv

The installation downloads PowerShell modules from the PowerShell Gallery.

The worker must have outbound HTTPS connectivity.

Step 3 — Verify installed modules

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-Module -ListAvailable Az.Accounts,Az.Resources | Select Name,Version" \
  --query "value[0].message" \
  --output tsv

Expected: Both Az.Accounts and Az.Resources are available.

Lab 10 — Create the PowerShell Runbook

Now we'll create our first runbook.

The runbook will perform two tasks:

  1. Write a local execution marker on the Hybrid Worker VM.

  2. Authenticate through managed identity and retrieve metadata about the Windows VM.

If either operation fails, the job should report an error.

Step 1 — Create the runbook source

cat > hybrid-resource-check.ps1 <<'POWERSHELL'
param(
    [Parameter(Mandatory = $true)]
    [string]$TargetVMName,

    [Parameter(Mandatory = $true)]
    [string]$TargetResourceGroup,

    [Parameter(Mandatory = $true)]
    [string]$SubscriptionId
)

$ErrorActionPreference = "Stop"

$Folder = "C:\ProgramData\AzureWorkshopz"
$Marker = Join-Path $Folder "last-status.txt"

New-Item -Path $Folder `
    -ItemType Directory -Force | Out-Null

Set-Content -Path $Marker -Value "STARTED"

Write-Output "================================"
Write-Output "AZURE HYBRID RUNBOOK WORKER LAB"
Write-Output "================================"
Write-Output "Computer: $env:COMPUTERNAME"
Write-Output "Time: $(Get-Date -Format o)"

try {
    Import-Module Az.Accounts -ErrorAction Stop
    Import-Module Az.Resources -ErrorAction Stop

    Disable-AzContextAutosave `
        -Scope Process | Out-Null

    Write-Output "Authenticating with managed identity..."

    $Context = (
        Connect-AzAccount `
            -Identity `
            -Subscription $SubscriptionId `
            -ErrorAction Stop
    ).Context

    Write-Output "Managed identity authentication succeeded."

    $ResourceId = "/subscriptions/$SubscriptionId/" +
                  "resourceGroups/$TargetResourceGroup/" +
                  "providers/Microsoft.Compute/virtualMachines/" +
                  $TargetVMName

    Write-Output "Reading Azure VM resource metadata..."

    $Resource = Get-AzResource `
        -ResourceId $ResourceId `
        -DefaultProfile $Context `
        -ErrorAction Stop

    if (-not $Resource) {
        throw "Target Azure VM not found."
    }

    Write-Output "VM name: $($Resource.Name)"
    Write-Output "Resource type: $($Resource.ResourceType)"
    Write-Output "Location: $($Resource.Location)"

    Set-Content -Path $Marker -Value "SUCCESS"

    Write-Output "RESULT: SUCCESS"
}
catch {
    Set-Content -Path $Marker -Value "FAILED"

    Write-Error "Runbook failed: $($_.Exception.Message)"

    throw
}
POWERSHELL

The runbook intentionally creates a local execution marker.

This proves that the job runs on the Hybrid Worker rather than only in the Azure Automation cloud sandbox.

Step 2 — Create the runbook resource

az automation runbook create \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --name "$RUNBOOK" \
  --type PowerShell \
  --location "$LOCATION" \
  --description "Hybrid Worker managed identity and Azure resource check"

Step 3 — Upload the content

az automation runbook replace-content \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --name "$RUNBOOK" \
  --content @hybrid-resource-check.ps1

Step 4 — Publish the runbook

az automation runbook publish \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --name "$RUNBOOK"

Step 5 — Verify publication

az automation runbook show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --name "$RUNBOOK" \
  --output json

Confirm that the runbook is published.

Checkpoint: Continue only when the runbook is published and the worker is registered and healthy.

Lab 11 — Execute the Runbook on the Hybrid Worker

We will now start the runbook on our Hybrid Worker Group.

Azure Automation provides a runOn property to target a Hybrid Worker Group.

We'll use Azure CLI with the documented Automation Job REST API to create a job with a predictable job ID.

Step 1 — Create a reusable job-start function

start_lab_job() {

  export JOB_ID=$(cat /proc/sys/kernel/random/uuid)

  cat > job-request.json <<EOF
{
  "properties": {
    "runbook": {
      "name": "$RUNBOOK"
    },
    "runOn": "$WORKER_GROUP",
    "parameters": {
      "TargetVMName": "$VM",
      "TargetResourceGroup": "$RG",
      "SubscriptionId": "$SUB_ID"
    }
  }
}
EOF

  az rest \
    --method put \
    --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
    --headers "Content-Type=application/json" \
    --body @job-request.json

  echo "Started job: $JOB_ID"
}

Step 2 — Start the first job

start_lab_job

Step 3 — Inspect job status

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
  --query "properties.{Status:status,RunOn:runOn,Started:startTime,Ended:endTime}" \
  --output json

Expected execution progression:

New
Activating
Running
Completed

The exact progression may vary.

Step 4 — Monitor job execution

for i in $(seq 1 30); do

  STATUS=$(az rest \
    --method get \
    --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
    --query "properties.status" \
    --output tsv)

  echo "Job Status: $STATUS"

  case "$STATUS" in
    Completed|Failed|Suspended|Stopped)
      break
      ;;
  esac

  sleep 20

done

Step 5 — Inspect job streams

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}/streams?api-version=${API_VERSION}" \
  --query "value[].{Type:properties.streamType,Summary:properties.summary}" \
  --output table

Expected successful messages include:

Managed identity authentication succeeded.
VM name: vm-hybrid-...
RESULT: SUCCESS

Step 6 — Verify local execution

Check the marker file on the Windows VM.

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-Content 'C:\ProgramData\AzureWorkshopz\last-status.txt'" \
  --query "value[0].message" \
  --output tsv

Expected:

SUCCESS

We have now demonstrated a successful Hybrid Runbook Worker execution.

The job was initiated from Azure Automation, executed on the Windows VM, authenticated to Azure, and returned resource information.

Lab 12 — BREAK #1: Remove Managed Identity Permissions

Now we'll simulate a common Azure operations incident.

An administrator removes the Reader role from the Automation Account's system-assigned identity.

The Hybrid Worker remains registered and online.

However, the runbook should lose permission to retrieve Azure resource information.

Step 1 — Review the current assignment

az role assignment list \
  --assignee "$AUTOMATION_PRINCIPAL_ID" \
  --scope "$RG_ID" \
  --output table

Confirm the Reader role exists.

Step 2 — Remove the role assignment

az role assignment delete \
  --ids "$READER_ASSIGNMENT_ID"

Step 3 — Verify removal

az role assignment list \
  --assignee "$AUTOMATION_PRINCIPAL_ID" \
  --scope "$RG_ID" \
  --output table

The lab-created Reader assignment should no longer appear.

Step 4 — Start another job

start_lab_job

Step 5 — Inspect status

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
  --query "properties.{Status:status,Exception:exception,RunOn:runOn}" \
  --output json

Expected behavior

The runbook may authenticate successfully but fail when attempting to read Azure resource metadata.

A typical error may contain:

AuthorizationFailed

The client does not have authorization to perform
Microsoft.Resources/subscriptions/resourceGroups/read
or Microsoft.Compute/virtualMachines/read.

The exact message depends on the Azure operation and authorization state.

Important — RBAC propagation

Azure RBAC permission changes are not always immediate.

Managed identity tokens and authorization state can be cached.

Removing the role assignment does not guarantee that the next job will fail instantly.

Allow time for the permission change to propagate and initiate a fresh job when testing.

Do not mistake a temporary successful execution for proof that the deleted role is still assigned.

Lab 13 — DIAGNOSE: Investigate the Failed Runbook

Imagine receiving the following incident:

Our Automation runbook worked yesterday but fails today. The Hybrid Runbook Worker VM is running, and the extension appears healthy.

How do we investigate?

Step 1 — Inspect the job status

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
  --output json

Check:

  • Status.

  • Runbook name.

  • Worker Group.

  • Start time.

  • End time.

  • Exception.

Step 2 — Inspect job stream errors

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}/streams?api-version=${API_VERSION}" \
  --output json

Look for a failed Get-AzResource operation or another authorization error.

Step 3 — Verify the Automation identity

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}?api-version=${API_VERSION}" \
  --query identity \
  --output json

Confirm that the Automation Account still has a system-assigned identity.

Step 4 — Inspect effective role assignments

az role assignment list \
  --assignee "$AUTOMATION_PRINCIPAL_ID" \
  --scope "$RG_ID" \
  --include-inherited \
  --output table

In the intended lab environment, the Automation Account identity no longer has Reader permission.

If another inherited role grants equivalent access, the failure might not occur. Check the effective permissions before drawing conclusions.

Step 5 — Verify VM identity

az vm identity show \
  --resource-group "$RG" \
  --name "$VM" \
  --output json

The VM identity should remain enabled.

Step 6 — Verify the Hybrid Worker extension

az vm extension show \
  --resource-group "$RG" \
  --vm-name "$VM" \
  --name "$EXTENSION_NAME" \
  --instance-view \
  --output json

Expected: Extension provisioning is still successful.

Step 7 — Check the local execution marker

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-Content 'C:\ProgramData\AzureWorkshopz\last-status.txt'" \
  --query "value[0].message" \
  --output tsv

After an authorization failure that reaches the runbook catch block, the marker should report:

FAILED

What have we identified?

The VM is healthy.

The Hybrid Worker extension is healthy.

The runbook can execute locally.

The failure happens when the runbook attempts an Azure Resource Manager operation using an identity without sufficient permissions.

This is an Azure RBAC authorization failure, not a Hybrid Worker registration failure.

Lab 14 — REPAIR: Restore Managed Identity Permissions

Now we'll repair the incident.

The correct action is to restore the required permission to the Automation Account managed identity.

We do not need to reinstall the Hybrid Worker extension or rebuild the VM.

Step 1 — Restore the Reader role

export READER_ASSIGNMENT_ID=$(az role assignment create \
  --assignee-object-id "$AUTOMATION_PRINCIPAL_ID" \
  --assignee-principal-type ServicePrincipal \
  --role "Reader" \
  --scope "$RG_ID" \
  --query id \
  --output tsv)

Step 2 — Verify permissions

az role assignment list \
  --assignee "$AUTOMATION_PRINCIPAL_ID" \
  --scope "$RG_ID" \
  --output table

Expected: Reader has been restored.

Step 3 — Start another job

start_lab_job

Step 4 — Verify execution

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
  --query "properties.{Status:status,Exception:exception}" \
  --output json

Allow time for RBAC propagation and the fresh job to complete.

Expected final status:

Completed

Step 5 — Verify local result

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-Content 'C:\ProgramData\AzureWorkshopz\last-status.txt'" \
  --query "value[0].message" \
  --output tsv

Expected:

SUCCESS

What did we learn?

The worker itself was not broken.

The Automation Account identity had lost access to an Azure resource.

Restoring the appropriate RBAC role repaired the runbook.

This is why identity, authorization, and worker health should be investigated independently.

Lab 15 — BREAK #2: Simulate a Hybrid Worker Service Outage

For the second incident, we'll stop the Hybrid Worker service on Windows.

This is different from removing Azure permissions.

The worker will no longer be able to poll Azure Automation for jobs while the service is stopped.

Step 1 — Verify the service

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-Service -Name HybridWorkerService" \
  --query "value[0].message" \
  --output tsv

Expected:

Running

Step 2 — Stop the Hybrid Worker service

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Stop-Service -Name HybridWorkerService -Force" \
  --query "value[0].message" \
  --output tsv

Step 3 — Verify the outage

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-Service -Name HybridWorkerService" \
  --query "value[0].message" \
  --output tsv

Expected:

Stopped

Step 4 — Start another runbook job

start_lab_job

Step 5 — Inspect job behavior

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
  --query "properties.{Status:status,RunOn:runOn}" \
  --output json

With no active worker processing jobs, the job may remain queued and eventually be suspended after the platform's retry behavior.

The exact timing is managed by Azure Automation.

Do not assume that stopping the service causes an immediate Failed status.

Step 6 — Inspect the worker registration

az automation hrwg hrw show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --hybrid-runbook-worker-group-name "$WORKER_GROUP" \
  --hybrid-runbook-worker-id "$WORKER_ID" \
  --output json

Look for heartbeat or availability information indicating that the worker is no longer actively reporting.

Important: Azure Run Command uses the Azure VM agent, not the Hybrid Worker service, so we can use it to restore the stopped service.

Lab 16 — REPAIR: Restart the Hybrid Worker Service

Step 1 — Restart the service

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Start-Service -Name HybridWorkerService" \
  --query "value[0].message" \
  --output tsv

Step 2 — Verify service state

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-Service -Name HybridWorkerService" \
  --query "value[0].message" \
  --output tsv

Expected:

Running

Step 3 — Verify worker communication

az automation hrwg hrw show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --hybrid-runbook-worker-group-name "$WORKER_GROUP" \
  --hybrid-runbook-worker-id "$WORKER_ID" \
  --output json

Verify that the worker resumes reporting.

Step 4 — Start a fresh job

start_lab_job

Step 5 — Verify recovery

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
  --query "properties.{Status:status,RunOn:runOn}" \
  --output json

Expected final state:

Completed

A job that was previously suspended is not guaranteed to resume automatically.

Starting a new job is the cleanest way to verify recovery in this workshop.

Lab 17 — Advanced Hybrid Worker Troubleshooting

Let's review the main operational checks.

Scenario 1 — Worker extension installation fails

Check the extension instance view:

az vm extension show \
  --resource-group "$RG" \
  --vm-name "$VM" \
  --name "$EXTENSION_NAME" \
  --instance-view \
  --output json

Common causes include:

  • System-assigned VM identity is not enabled.

  • Incorrect Automation Hybrid Service URL.

  • Missing outbound HTTPS connectivity.

  • Unsupported operating system.

  • Hybrid Worker Group does not exist.

  • VM agent or extension provisioning failure.

Scenario 2 — Worker service is not running

az vm run-command invoke \
  -g "$RG" \
  -n "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-Service HybridWorkerService | Format-List *" \
  --query "value[0].message" \
  -o tsv

Scenario 3 — Inspect Windows Hybrid Worker logs

Microsoft documents worker logs under the Windows Azure VM extension logging directory.

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-ChildItem 'C:\WindowsAzure\Logs\Plugins\Microsoft.Azure.Automation.HybridWorker.HybridWorkerForWindows' -Recurse -File | Select-Object -Last 20 FullName" \
  --query "value[0].message" \
  --output tsv

Scenario 4 — Inspect the Microsoft-SMA Operational log

az vm run-command invoke \
  --resource-group "$RG" \
  --name "$VM" \
  --command-id RunPowerShellScript \
  --scripts \
    "Get-WinEvent -LogName 'Microsoft-SMA/Operational' -MaxEvents 20 | Select-Object TimeCreated,Id,LevelDisplayName,Message" \
  --query "value[0].message" \
  --output tsv

This log can provide information about runbook execution and Hybrid Worker operational problems.

Scenario 5 — Runbook authentication fails

Check the Automation Account identity:

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}?api-version=${API_VERSION}" \
  --query identity \
  --output json

Inspect assigned roles:

az role assignment list \
  --assignee "$AUTOMATION_PRINCIPAL_ID" \
  --include-inherited \
  --output table

Review:

  • Is the correct identity enabled?

  • Does the identity have access to the target resource?

  • Is the assignment scoped correctly?

  • Has RBAC propagation completed?

  • Are the Az PowerShell modules installed on the worker?

  • Does the runbook use the intended subscription?

Scenario 6 — No jobs are being processed

Check:

  1. Worker VM power state.

  2. VM system-assigned identity.

  3. Hybrid Worker extension provisioning.

  4. Hybrid Worker service status.

  5. Outbound connectivity to required Azure Automation endpoints.

  6. Hybrid Worker Group registration.

  7. Job targeting configuration.

An unhealthy worker can result in jobs remaining queued or being suspended.

Lab 18 — Final Verification

Before deleting the workshop, let's verify that everything is working.

Step 1 — Verify the Automation Account

az automation account show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --output json

Step 2 — Verify identities

az vm identity show \
  --resource-group "$RG" \
  --name "$VM" \
  --output json
az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}?api-version=${API_VERSION}" \
  --query identity \
  --output json

Step 3 — Verify the worker

az automation hrwg hrw list \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --hybrid-runbook-worker-group-name "$WORKER_GROUP" \
  --output json

Step 4 — Verify the runbook

az automation runbook show \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --name "$RUNBOOK" \
  --output json

Step 5 — Verify permissions

az role assignment list \
  --assignee "$AUTOMATION_PRINCIPAL_ID" \
  --scope "$RG_ID" \
  --output table

Step 6 — Run the final job

start_lab_job

Check the final status after execution:

az rest \
  --method get \
  --uri "https://management.azure.com${AUTOMATION_ID}/jobs/${JOB_ID}?api-version=${API_VERSION}" \
  --query "properties.status" \
  --output tsv

Expected:

Completed

Final validation checklist

Component

Expected state

Automation Account

Available

Automation Account identity

Enabled

Worker VM

Running

VM managed identity

Enabled

Hybrid Worker Group

Created

Hybrid Worker extension

Succeeded

HybridWorkerService

Running

PowerShell modules

Installed

Runbook

Published

Automation identity RBAC

Reader

Latest job

Completed

Local marker file

SUCCESS

Lab 19 — Cleanup

Azure virtual machines, managed disks, and public IP addresses can continue generating charges.

Delete the workshop resources after completing the exercises.

Step 1 — Review deployed resources

az resource list \
  --resource-group "$RG" \
  --query "[].{Name:name,Type:type}" \
  --output table

Step 2 — Remove the Hybrid Worker extension

az vm extension delete \
  --resource-group "$RG" \
  --vm-name "$VM" \
  --name "$EXTENSION_NAME"

Step 3 — Delete the worker registration

az automation hrwg hrw delete \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --hybrid-runbook-worker-group-name "$WORKER_GROUP" \
  --hybrid-runbook-worker-id "$WORKER_ID"

Step 4 — Delete the Worker Group

az automation hrwg delete \
  --resource-group "$RG" \
  --automation-account-name "$AUTOMATION" \
  --name "$WORKER_GROUP"

Step 5 — Delete the resource group

az group delete \
  --name "$RG" \
  --yes

This removes the Automation Account, VM, networking, identities associated with those resources, and the lab's other resources.

Step 6 — Verify deletion

az group exists \
  --name "$RG"

Expected:

false

Step 7 — Remove local workshop files

rm -f hybrid-settings.json
rm -f worker-setup.ps1
rm -f hybrid-resource-check.ps1
rm -f job-request.json

Cleanup complete.

Workshop Review — What Have We Learned?

Azure technology

Learning outcome

Azure Automation

Centralized runbook management

Hybrid Runbook Worker

Running automation outside Azure's cloud sandbox

Hybrid Worker Groups

Group-based job targeting

VM Extensions

Extension-based worker deployment

Managed Identity

Passwordless Azure authentication

Azure RBAC

Least-privilege authorization

Windows PowerShell

Local automation execution

Az.Accounts / Az.Resources

Azure resource access from runbooks

Azure Run Command

Guest-level administration

Automation Job API

Starting and inspecting jobs

Worker Service

Local worker processing

Azure CLI

End-to-end deployment and troubleshooting

Two failures, two different root causes

Failure 1 — Managed Identity Permissions

The Hybrid Worker was online.

The runbook was executing.

But Azure RBAC denied the runbook's resource query because the Automation Account identity lacked the required role.

The solution was to restore the appropriate RBAC assignment.

Failure 2 — Hybrid Worker Service

The Automation Account and its permissions were healthy.

But the local worker service was stopped.

No active worker was available to process the requested jobs.

The solution was to restore the worker service and verify a fresh execution.

These are fundamentally different failures, even if both are reported as automation not working.

Final Challenge — Test Your Knowledge

Before completing the workshop, consider these questions:

  1. What is the difference between Azure Automation cloud runbooks and Hybrid Runbook Workers?

  2. Why does an extension-based Hybrid Worker require a system-assigned VM identity?

  3. Which identity is used for Azure authentication when the Automation Account has a system-assigned managed identity?

  4. Why should a read-only runbook not receive Contributor permissions?

  5. What is the purpose of a Hybrid Worker Group?

  6. Does the Hybrid Worker VM need inbound RDP access to execute runbooks?

  7. Why must PowerShell modules be installed locally on a Hybrid Worker?

  8. What happens when the Automation Account identity loses permission to read an Azure resource?

  9. Why can RBAC changes take time to affect job execution?

  10. How can you distinguish a permissions failure from an unavailable Hybrid Worker?

  11. What happens when no worker in a group is actively processing jobs?

  12. Why is successful extension provisioning not enough to guarantee successful automation?

Bonus Challenges

Challenge 1 — Add a second Hybrid Worker

Deploy another VM into the same Worker Group. Test job distribution and worker availability when one VM is offline.

Challenge 2 — Implement scheduled automation

Create a recurring schedule, link it to the published runbook, and target the Hybrid Worker Group.

Challenge 3 — Automate VM operations

Extend the runbook to start or stop a dedicated test VM using a tightly scoped virtual machine management role.

Challenge 4 — Integrate Key Vault

Store an application secret in Azure Key Vault, allow the Automation Account identity to read it, and simulate an RBAC failure.

Challenge 5 — Centralized monitoring

Send Azure Automation job logs to Log Analytics and build KQL queries that identify failed executions.

Challenge 6 — Extend to Azure Arc

Replace the Azure VM worker with an Azure Arc-enabled server to demonstrate automation against non-Azure or on-premises infrastructure.

Conclusion

Azure Automation provides a powerful way to standardize operational tasks across cloud and hybrid environments.

However, successful automation depends on more than having a working script.

The Automation Account, Hybrid Worker registration, VM extension, managed identities, Azure RBAC permissions, network connectivity, and local execution environment must all work together.

In this workshop, we deployed an Azure Automation Account, configured managed identities, registered an extension-based Hybrid Runbook Worker, and created a PowerShell runbook that performed both local and Azure operations.

We then deliberately introduced two different failures.

First, we removed the Automation Account identity's Reader permissions and investigated the resulting authorization failure.

Next, we stopped the Hybrid Worker service and examined the impact on job processing.

In both cases, we restored the affected component and verified recovery through a new runbook execution.

The most important lesson is that a healthy Hybrid Runbook Worker does not guarantee successful runbook execution — and a failed runbook does not necessarily mean the worker is unhealthy.

Understanding the difference between identity, authorization, worker availability, and execution dependencies is essential for reliable Azure operations.

Deploy. Automate. Break. Diagnose. Repair. Verify.

Official Microsoft Documentation

Azure Workshopz | Beyond Cloud with Chriz

Hands-on Azure infrastructure workshops focused on automation, monitoring, troubleshooting, and recovery.

 
 
 

Comments


bottom of page