如何以编程方式使用 C# 获取或更新或切换或操作 Azure 门户中选定的 Azure VM 的自动关闭参数?

How to programmatically using C# fetch or update or toggle or manipulate the auto-shutdown parameters for a selected azure VM in Azure portal?

我正在尝试使用 C# 以编程方式从 Azure 门户获取选定 VM 的自动关闭参数的详细信息。我想要实现的目标如下:

  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

我希望通过 C# 程序完成此操作。

我不知道如何通过谷歌搜索来实现它。请提供详细的分步指南,因为我是编码、C# 和 AZURE 的新手

请注意,我们项目中的 VM 不是在任何开发测试实验室中创建的,它们是通过 LCS 直接创建的,并且在创建时使用 DEMO env 选项。

考虑到以上几点,您能否提供详细信息?或者这是不可能的,因为步骤不正确?

如果需要我提供任何其他信息以便为我提供解决方案,请告诉我。

我已经查看了以下 PowerShell 脚本:

但这似乎涉及在 DEV TEST 实验室中创建的 VM,在我的情况下,这将不起作用,因为我们的 VM 不是在单独的实验室中创建的,上面已尝试解释。因此我认为脚本不起作用

尝试查看一些 REST API 但也找不到任何内容。

正如您所注意到的,官方不支持在 DevTest Labs 之外的 VM 中访问此功能。有一个可用的端点用于读取和更新计划。但是,请务必注意,这目前不是官方支持的端点,因此 它可能随时更改或停止工作

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

如果我在 C# 中使用简单的 HttpClient 调用此端点,一旦我获得授权令牌,它将看起来像这样:

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();
    }
}