This snippet will allow you to re-run any Azure Automation Runbook job with the same parameters and in the same context (Azure or Hybrid Worker Group).
# Set the variables from the previous job
$AutomationAccountName = ''
$ResourceGroupName = ''
$JobId = ''
# Get the previous job
$AutoAccount = @{
AutomationAccountName = $AutomationAccountName
ResourceGroupName = $ResourceGroupName
}
$PreviousJob = Get-AzAutomationJob @AutoAccount -Id $JobId
# Build the parameters to resubmit
$StartRunbook = $AutoAccount.Clone()
# Add Runbook Name
$StartRunbook.Add('Name',$PreviousJob.RunbookName)
# If Hybrid Worker used, add it
if(-not [string]::IsNullOrEmpty($PreviousJob.HybridWorker)){
$StartRunbook.Add('RunOn',$PreviousJob.HybridWorker)
}
# Create a hashtable with the parameters
if($PreviousJob.JobParameters.Count -gt 0){
$Parameters = @{}
$PreviousJob.JobParameters.GetEnumerator() | ForEach-Object{
$Parameters.Add($_.Key,$_.Value)
}
$StartRunbook.Add('Parameters',$Parameters)
}
# Start the job
$RBjob = Start-AzAutomationRunbook @StartRunbook
$RBjob