由于 VM 部署了资源管理器,因此在不同的提供程序(计算和网络)下状态、IP 地址和大小信息。目前可能无法在通话中获取虚拟机信息和网络信息。
使用Microsoft Azure Management Client Library (Fluent),我们可以获得VM信息(电源状态,机器大小,IP地址)。实际上,它调用了 REST API 两次。关于 Azure 认证请参考how to create an authentication file。
AzureCredentials credentials = AzureCredentials.FromFile("Full path of your AzureAuthFile");
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC)
.Authenticate(credentials)
.WithDefaultSubscription();
foreach (var virtualMachine in azure.VirtualMachines.ListByGroup("Your Resource Group Name").Where(virtualMachine => virtualMachine.ComputerName.Equals("vmName")))
{
var state = virtualMachine.PowerState;
var size = virtualMachine.Size;
var ip = virtualMachine.GetPrimaryPublicIpAddress().IpAddress; //call Rest API again
}
如果它部署在 CloudService 下,那么我们可以使用Windows Azure management library。很容易得到VM(Role)
有关电源状态、IP 地址和机器大小的信息。
var certificate = new CertificateCloudCredentials(subscriptionId, x509Certificate);
var computeManagementClient = new ComputeManagementClient(certificate);
var deployments = await computeManagementClient.Deployments.GetByNameAsync (hostedServiceName,"Your Deployment Name");
var state = deployments.RoleInstances.First().PowerState;
var ipAddress = deployments.RoleInstances.First().IPAddress;
var size = deployments.RoleInstances.First().InstanceSize;