Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I know how to create and link a schedule via the Azure portal to an existing runbook. But now I want to add it via C# code.

The runbook works and doesn't need any change. There is a parameter with the name "Id", which I can enter when using the portal.

A lot of information I found (even from 2023) is telling me to use the Microsoft.Azure.Management.Automation, but this one is deprecated and I see a lot of notes mentioning the Azure.ResourceManager.Automation. Since I don't want any problems with old packages, I use the new one.

That brings me to the following code:

C#
public async Task CreateSchedule(DateTime date, int id)
{
    if (date < DateTime.Now.AddMinutes(10))
        throw new Exception("The run date should be at least 10 minutes from now.");

    if (id <= 0)
        throw new ArgumentException($"The {nameof(id)} can't be 0 or less.");

    ArmClient armClient = new(new DefaultAzureCredential());

    var subscription = armClient.GetDefaultSubscription();

    var automationAccount = subscription.GetAutomationAccounts().First();
    Azure.Response<AutomationRunbookResource> runbook = automationAccount.GetAutomationRunbook("TheRunbook001");
    
    AutomationScheduleData scheduleData = new AutomationScheduleData
    {
        StartOn = date,
        IsEnabled = true,
        ExpireOn = DateTime.Now.AddMonths(1),
        Frequency = Azure.ResourceManager.Automation.Models.AutomationScheduleFrequency.OneTime,
        TimeZone = "Indochina Time"
    };
}

But I get stuck here. I do get to see the found runbook, but that's all of it.

How do I add a schedule to the found runbook? There is no method called "AddSchedule" or "CreateOrUpdate", like some websites tell me.

What I have tried:

I tried to Google the answer and asked ChatGPT, but the only results I get are for the resource groups or deprecated version.
Posted
Updated 10-Feb-23 6:34am

Look at this documentation
AutomationRunbookCollection Class (Azure.ResourceManager.Automation) - Azure for .NET Developers | Microsoft Learn[^]

It looks like there is CreateOrUpdate method in this AutomationRunbookCollection class. You can get instance of it from GetAutomationRunbooks method from automationAccount object. I found it strange that single runbook object can not be CreateOrUpdate()-ed, but collection of them can be, I'll try it in the next days and will update you.
 
Share this answer
 
Comments
Kenji Elzerman 10-Feb-23 14:06pm    
Thank you for your reply. I continued my search for this as well and I followed the path with the Azure Management API, but it gave me errors in the last step, where I used the URL for connecting the Runbook and the schedule. I too will look into your suggestion as well, but if you want I could update the questions with my new findings. Let me know.
I think that this is what you are looking for: Job Schedule - Create - REST API (Azure Automation) | Microsoft Learn[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900