我正在尝试使用 C# 以编程方式从 Azure 门户获取选定虚拟机的自动关闭参数的详细信息。我想要实现的目标如下:
- First, get the auto shut down status it is enabled or disabled?
- If it is enabled then get auto shutdown time and its time zone related information
- Based on input update the timezone and time or disable the auto shutdown status on need basis
我希望通过 C# 程序完成此操作。
我不知道如何通过我所做的谷歌搜索来实现它。请提供详细的分步指南,因为我是编码、C# 和 AZURE 的新手,因此如何实现它
请注意,我们项目中的虚拟机不是在任何开发测试实验室中创建的,这些虚拟机是直接通过 LCS 创建的,并在创建时使用 DEMO env 选项。
考虑到上述几点,您能否提供详细信息?或者这是不可能的,因为步骤不正确?
如果我需要任何其他信息,请告诉我,以便您为我提供解决方案。
我已经研究过下面的 PowerShell 脚本:
How to collect the Azure VM auto-shutdown time using PowerShell?
但这似乎涉及在 DEV TEST 实验室中创建的虚拟机,在我的情况下,该虚拟机无法工作,因为我们的虚拟机不是在单独的实验室中创建的,已在上面尝试解释过。因此我认为该脚本不起作用
尝试研究一些 REST API,但也找不到任何东西。
最佳答案
正如您所注意到的,官方不支持在开发测试实验室之外的虚拟机中访问此功能。有一个可用的端点用于读取和更新时间表。但请务必注意,这目前不是官方支持的端点,因此它可能随时更改或停止工作。
端点是:
https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/schedules/shutdown-computevm-{vmName}?api-version=2018-10-15 -预览
如果我使用 C# 中的简单 HttpClient 调用此端点,一旦我获得了授权 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();
}
}
关于c# - 如何以编程方式使用 C# 获取、更新、切换或操作 Azure 门户中选定的 azure VM 的自动关闭参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56747494/