1
votes

I am trying to programmatically using C# fetch the details of Auto-shutdown parameters for a selected VM from azure portal. The things i want to achieve are given below:

  1. First, get the auto shut down status it is enabled or disabled?
  2. If it is enabled then get auto shutdown time and its time zone related information
  3. Based on input update the timezone and time or disable the auto shutdown status on need basis

I want this to be done via C# program.

I am not knowing how to achieve it through the googling i have done. Please provide a detailed step by step guide how to achieve it as i am new to coding, C#, and AZURE

Please note that the VM's in our project are not created in any DevTest labs these are created through LCS directly and with DEMO env an option while creation.

Can you please provide details taking the above points into consideration? Or this is not possible as the step is not correct?

Please let me know if any other information is needed from my end to enable you provide me a solution.

I have already looked into below PowerShell script:

How to collect the Azure VM auto-shutdown time using PowerShell?

But this seems to be involving a VM created in DEV TEST lab which in my case will not work as our VM'S are not created in a separate lab has tried to explain above. Hence i think the script does not work

Tried to look into a few REST API but could not find anything there also.

1

1 Answers

2
votes

As you have noticed, accessing this feature in VMs outside of DevTest Labs is not officially supported. There is an available endpoint for reading and updating the schedule. However it is very important to note this is not currently an officially supported endpoint so it may change or stop working at any time.

The endpoint is: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/schedules/shutdown-computevm-{vmName}?api-version=2018-10-15-preview

If I were to call this endpoint using a simple HttpClient in C# it would look something like this once I've obtained an authorization token:

class Program
{
    private static string bearerToken = Configuration.Token;
    private static string subscriptionId = Configuration.SubscriptionId;
    private static string resourceGroupName = Configuration.ResourceGroup;
    private static string vmName = Configuration.VMName;

    static void Main(string[] args)
    {
        using(var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", bearerToken);

            var result = client.GetStringAsync(new Uri($"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/schedules/shutdown-computevm-{vmName}?api-version=2018-10-15-preview")).Result;

            Console.WriteLine(result);
        }

        Console.ReadLine();
    }
}