Search code examples
azurepowershellazure-automationazure-runbook

How to create automation schedule for Azure Automation Runbook from PowerShell to be run on Hybrid Worker?


I want to create automation schedule for Azure Automation Runbook from PowerShell. I don't want it to be run by default on Azure, but on Hybrid Worker, which is present in my Hybrid worker groups. So I have that commands:

Import-AzureRmAutomationRunbook -Name $runbookName `
            -Path $scriptPath `
            -ResourceGroupName $automationResourceGroupName `
            -AutomationAccountName $automationAccountName  `
            -Type PowerShellWorkflow
    
Publish-AzureRmAutomationRunbook -Name $runbookName `
            -AutomationAccountName $automationAccountName  `
            -ResourceGroupName $automationResourceGroupName

New-AzureRmAutomationSchedule -Name $runbookName ` 
            -AutomationAccountName $automationAccountName `
            -StartTime $StartTime `
            -ExpiryTime $EndTime `
            -DayInterval 1 `
            -ResourceGroupName $automationResourceGroupName

It can be done manually from the Azure portal:

runbook manual scheduler

but I need it to be done from PowerShell. I couldn't find it on MS docs.


Solution

  • If you are using the AzureRm module, just use the Start-AzureRmAutomationRunbook, specify the -RunOn parameter with the name of your Hybrid Worker group.

    Start-AzureRmAutomationRunbook –AutomationAccountName "MyAutomationAccount" –Name "Test-Runbook" -RunOn "MyHybridGroup"
    

    Reference(it uses the new Az command) - https://learn.microsoft.com/en-us/azure/automation/automation-hrw-run-runbooks#start-a-runbook-on-a-hybrid-runbook-worker

    Update:

    To schedule the runbook, you could use Register-AzureRmAutomationScheduledRunbook, specify the -RunOn parameter.

    Register-AzureRmAutomationScheduledRunbook -AutomationAccountName "Contoso17" -Name "Runbk01" -ScheduleName "Sched01" -ResourceGroupName "ResourceGroup01" -RunOn "MyHybridGroup"