检查Azure VM是否正在运行

时间:2014-08-14 23:25:37

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

我想查询现有的azure虚拟机,以检查它是否已完全部署并能够远程连接。有没有办法使用Azure Rest API执行此操作?

我已经概述了我目前的流程以及为什么我希望在下面发出这样的电话。

我使用Windows Azure management library使用ComputeManagementClientCreateDeploymentAsync功能创建VM。然后,这将返回状态Succeeded,如果我然后执行部署,则其状态为DeploymentStatus.Running

运行之后我正在尝试创建远程PowerShell连接,以便我可以格式化磁盘。由于无法创建会话,我一直收到错误。

  

WinRM无法完成操作。验证指定的计算机名称是否有效,计算机是否可通过网络访问,以及是否启用了WinRM服务的防火墙例外,并允许从此计算机进行访问。默认情况下,公共配置文件的WinRM防火墙例外限制对同一本地子网内的远程计算机的访问。有关详细信息,请参阅about_Remote_Troubleshooting帮助主题。

如果我进入azure管理门户,状态是配置,我认为这就是我无法创建会话的原因。

过程

CreateAzureVM()
CheckAzureVmCanBeConnectedTo() //I am unable to do this correctly
RunPowershellScriptOnVM() // this fails

3 个答案:

答案 0 :(得分:1)

您应该能够使用Get Deployment来轮询角色的InstanceStatus(请注意InstanceStatus!= Status)。当InstanceStatus是ReadyRole时,您应该能够登录您的机器。

部署状态和角色InstanceStatus之间存在区别。可以将角色视为实际的虚拟机,而部署可以描述多个虚拟机。

答案 1 :(得分:1)

SubscriptionCloudCredentials certificate = new CertificateCloudCredentials(subscriptionId, x509Certificate);

IComputeManagementClient ComputeManagementClient = new ComputeManagementClient(certificate);

var deployments = ComputeManagementClient.Deployments.GetBySlot(hostedServiceName, DeploymentSlot.YourDeploymentSlot);

var state = deployments.RoleInstances.First().PowerState;

I hope that this will help you.

答案 2 :(得分:0)

如果没有看到您的代码,很难确切地说您需要做什么,但我建议您使用await关键字等待完成对Azure API的调用以创建VM。请参阅本MSDN指南中“部署虚拟机”下的代码示例:http://msdn.microsoft.com/en-us/library/azure/dn722415.aspx#bk_createres