如何在天蓝色的VM上启动?

时间:2016-05-24 19:54:04

标签: c# azure azure-virtual-machine azure-management

标题说明了一切。我尝试了很多东西,但我认为其中任何一个都不值得一提。我终于想通了避免Microsoft.WindowsAzure并安装了Microsoft.Azure.Management.Compute和Microsoft.Azure.Common库。

我终于得到了这样的身份验证令牌:

var authenticationContext = new AuthenticationContext("https://login.windows.net/deadbeef-beef-beef-beef-ec74557498e8");
var credential = new ClientCredential("beefbeef-beef-beef-beef-b1d3cf5d037d", "passwordpasswordpasswordpasswordpasswordpas=");
var result = authenticationContext.AcquireTokenAsync("https://www.url.com/servicename", credential);

但是现在我正在努力使用这些文档来学习启动我的虚拟机。我甚至不确定从哪里开始。我所知道的是,我想避免使用REST API并将代码保存在C#中。我正在寻找类似的东西:

using (var client = new ComputeManagementClient(creds)) {
    foreach (var vm in client.VMs)
    {
        Console.WriteLine("Starting VM: {0}", vm.Name);

        vm.PowerOn();
    }
}

1 个答案:

答案 0 :(得分:1)

假设您正在处理基于ARM的虚拟机,请参阅启动虚拟机的代码。 "上下文"是Microsoft.Azure.Management.Compute命名空间下的ComputeManagementClient

var result = VirtualMachinesOperationsExtensions.Start(context.VirtualMachines, azureResourceGroup, azureResourceName);

如果您正在处理经典虚拟机,请在此处输入启动虚拟机的代码。 "上下文"是Microsoft.WindowsAzure.Management.Compute命名空间下的ComputeManagementClient

var result = context.VirtualMachines.BeginStarting(serviceName, deploymentName,
                        instanceName);

您还可以避免编写自己的代码并对其进行监控以确保一切正常并使用CloudMonix来安排Azure VM的启动和关闭时的所有麻烦。 (我附属于该服务)