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:
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.