Search code examples
azureazure-sdk-.netazure-automationazure-sdk

Disable Azure Automation Runbook Schedule using .net SDK


I am trying to disable a Runbook schedule using .NET SDK Retrieved the JobScheduled i want to disable and tried setting the associated runbook and schedule to null and "".

                    var schedulenm = new ScheduleAssociationProperty();
                    schedulenm.Name = "";
                    var runbooknm = new RunbookAssociationProperty();
                    runbooknm.Name = "";
                    jocsched.Properties.Schedule = schedulenm;
                    jobsched.Properties.Runbook = runbooknm;

Also tried directly querying the main schedule and set the IsEnabled property to false. However that also doesnt have any impact.

What is the correct way to disable the schedule associated with a runbook? ( just want it disabled not deleted)


Solution

  • According to your description, if you want to disable the schedule associated with a runbook. You could use AutomationManagementClient.JobSchedules.Delete method.

    The JobSchedules means the relationship between the runbook and schedule.

    After calling this method, the runbook will not associate with schedule, but it will not delete the schedule.

    More details, you could refer to below code sample:

         var r2 =  automationManagementClient.JobSchedules.List("groupname", "accountname").JobSchedules.First();
    
    
            automationManagementClient.JobSchedules.Delete("groupname", "accountname", r2.Properties.Id);
    

    Result:

    You could see the schedule still existed.

    Image1:

    enter image description here

    Image2:

    enter image description here

    Would that be the exact equivalent of setting the 'Enabled' property to No in the UI?

    No, if you want to disable the schedule, you should use AutomationManagementClient.Schedules.Patch method.

    More details, you could refer to this codes:

            AutomationManagementClient automationManagementClient = new AutomationManagementClient(aadTokenCredentials, resourceManagerUri);
    
    
            SchedulePatchParameters p1 = new SchedulePatchParameters("yourSchedulename");
    
    
            SchedulePatchProperties p2 = new SchedulePatchProperties();
    
    
            p2.IsEnabled = false;
    
            p1.Properties = p2;
    
            var result = automationManagementClient.Schedules.Patch("rgname", "am accountname", p1).StatusCode;
    

    Result: enter image description here