Search code examples
azureazure-automation

Azure Automation Managment not able to execute run book


I am using Azure Automation Management 2.0.1. I am not able find Start method for Runbooks to execute a runbook. How do this with 2.0.1

var client = new Microsoft.Azure.Management.Automation.AutomationManagementClient(new CertificateCloudCredentials(subscriptionId, cert));
var ct = new CancellationToken();

var content = await client.Runbooks.ListByNameAsync("MyAutomationAccountName", "MyRunbookName", ct);

var firstOrDefault = content?.Runbooks.FirstOrDefault();
if (firstOrDefault != null)
{
    var operation = client.Runbooks.Start("MyAutomationAccountName", new RunbookStartParameters(firstOrDefault.Id));
}

Solution

  • You need to use the automationManagementClient.Jobs.Create

    public static JobCreateResponse Create(
        this IJobOperations operations,
        string resourceGroupName,
        string automationAccount,
        JobCreateParameters parameters
    )
    

    You can find a full sample here this would be the relevant part -

    private void JobStart_Click(object sender, RoutedEventArgs e)
     {
            // Check for runbook name
            if (String.IsNullOrWhiteSpace(RunbookName.Text) || String.IsNullOrWhiteSpace(PublishState.Text)) throw new ArgumentNullException(RunbookName.Text);
    
            // Create job create parameters
            var jcparam = new JobCreateParameters
            {
                Properties = new JobCreateProperties
                {
                    Runbook = new RunbookAssociationProperty
                    {
                        // associate the runbook name
                        Name = RunbookName.Text
                    },
    
                    // pass parameters to runbook if any
                    Parameters = null
                }
            };
    
            // create runbook job. This gives back JobId
            var job = automationManagementClient.Jobs.Create(this.automationAccountName, jcparam).Job;
    
            JobGuid.Text = JobId.Text = job.Properties.JobId.ToString();
    
            Log.Text += (String.Format("\nJob Started for Runbook {0} , JobId {1}", RunbookName.Text, JobId.Text));
    }