使用 Azure SDK 获取 Azure 中 VM 实例的详细信息
本文关键字:Azure 实例 详细信息 VM SDK 获取 使用 | 更新日期: 2023-09-27 18:35:22
我需要使用 Azure SDK API 获取 Azure 中虚拟机实例的详细信息。
这些细节包括各种参数,例如...
*主机名
*IP地址
* 实例名称
*位置
* 标签名称等
Azure 中有两种类型的 VM...
* 经典 VM(通过旧门户)
* 虚拟机(使用新门户)
经典 VM 曾经使用相同名称创建云服务。
我能够使用 Microsoft.WindowsAzure.Management.Compute 库和 HostedService 类获取上述经典 VM 的参数详细信息。
代码片段:
var cert = new X509Certificate2(Constants.PFXCertificatePath, Constants.CertificatePassword);
var cred = new CertificateCloudCredentials(Constants.SubscriptionID, cert);
var computeClient = CloudContext.Clients.CreateComputeManagementClient(cred);
List<HostedServiceListResponse.HostedService> hostedServiceOperations = computeClient.HostedServices.List().HostedServices.ToList<HostedServiceListResponse.HostedService>();
foreach (HostedServiceListResponse.HostedService hostedService in hostedServiceOperations)
{
HostedServiceGetDetailedResponse detailedResponse = computeClient.HostedServices.GetDetailed(hostedService.ServiceName);
List<HostedServiceGetDetailedResponse.Deployment> deployments = detailedResponse.Deployments.
ToList<HostedServiceGetDetailedResponse.Deployment>();
foreach (HostedServiceGetDetailedResponse.Deployment deployment in deployments)
{
List<RoleInstance> roleInstances = deployment.RoleInstances.ToList<RoleInstance>();
foreach (RoleInstance roleInstance in roleInstances)
{
string privateId = deployment.PrivateId;
Console.WriteLine($"Host Name: {roleInstance.HostName}");
Console.WriteLine($"Instance Name: {roleInstance.InstanceName}");
Console.WriteLine($"Instance size: {roleInstance.InstanceSize}");
Console.WriteLine($"Private IP: {roleInstance.IPAddress}");
foreach (RoleInstance.PublicIP publicIP in roleInstance.PublicIPs)
{
Console.WriteLine($"Public IP: {publicIP.Address}");
}
Console.WriteLine($"Role Name: {roleInstance.RoleName}");
Console.WriteLine($"Power state: {roleInstance.PowerState}");
Console.WriteLine($"Instance status: {roleInstance.InstanceStatus}");
foreach (InstanceEndpoint instanceEndpoint in roleInstance.InstanceEndpoints)
{
Console.WriteLine($"Instance endpoint name: {instanceEndpoint.Name}, port: {instanceEndpoint.Port}, protocol: {instanceEndpoint.Protocol}");
Console.WriteLine($"Instance Virtual IP address: {instanceEndpoint.VirtualIPAddress}, local port: {instanceEndpoint.LocalPort}");
}
}
}
}
但是,我发现此方法不适用于新 VM(即从新门户创建的 VM),因为未为其创建云服务。
使用新的ARM库(Microsoft.Azure.Management.Resources),我能够获取此VM的基本信息,这些信息仅限于使用GenericResource类的名称和位置信息。
var token = GetAuthorizationHeader(Constants.TenantID);//Uses Active Directory authentication token
var tokenCred = new Microsoft.Rest.TokenCredentials(token);
ResourceManagementClient resourceClient = new ResourceManagementClient(tokenCred);
resourceClient.SubscriptionId = Constants.SubscriptionID;
var resourceGroups = resourceClient.ResourceGroups;
IResourcesOperations resources = resourceClient.Resources;
List<GenericResource> vmResources = resources.List().Where(r => r.Type == "Microsoft.Compute/virtualMachines").ToList<GenericResource>();
foreach (GenericResource resource in vmResources)
{
Console.WriteLine($"Resource Name: {resource.Name}, Resource Location: {resource.Location},Resource Plan: {resource.Plan}");
}
我使用此代码来获取实例信息:
public static void GetVirtualMachine(TokenCredentials credential, string groupName, string vmName, string subscriptionId)
{
Console.WriteLine("Getting information about the virtual machine...");
var computeManagementClient = new ComputeManagementClient(credential);
computeManagementClient.SubscriptionId = subscriptionId;
var vmResult = computeManagementClient.VirtualMachines.Get(groupName, vmName, "instanceview");
Console.WriteLine("hardwareProfile");
Console.WriteLine(" vmSize: " + vmResult.HardwareProfile.VmSize);
Console.WriteLine("'nstorageProfile");
Console.WriteLine(" imageReference");
Console.WriteLine(" publisher: " + vmResult.StorageProfile.ImageReference.Publisher);
Console.WriteLine(" offer: " + vmResult.StorageProfile.ImageReference.Offer);
Console.WriteLine(" sku: " + vmResult.StorageProfile.ImageReference.Sku);
Console.WriteLine(" version: " + vmResult.StorageProfile.ImageReference.Version);
Console.WriteLine(" osDisk");
Console.WriteLine(" osType: " + vmResult.StorageProfile.OsDisk.OsType);
Console.WriteLine(" name: " + vmResult.StorageProfile.OsDisk.Name);
Console.WriteLine(" createOption: " + vmResult.StorageProfile.OsDisk.CreateOption);
Console.WriteLine(" uri: " + vmResult.StorageProfile.OsDisk.Vhd.Uri);
Console.WriteLine(" caching: " + vmResult.StorageProfile.OsDisk.Caching);
Console.WriteLine("'nosProfile");
Console.WriteLine(" computerName: " + vmResult.OsProfile.ComputerName);
Console.WriteLine(" adminUsername: " + vmResult.OsProfile.AdminUsername);
Console.WriteLine(" provisionVMAgent: " + vmResult.OsProfile.WindowsConfiguration.ProvisionVMAgent.Value);
Console.WriteLine(" enableAutomaticUpdates: " + vmResult.OsProfile.WindowsConfiguration.EnableAutomaticUpdates.Value);
Console.WriteLine("'nnetworkProfile");
foreach (NetworkInterfaceReference nic in vmResult.NetworkProfile.NetworkInterfaces)
{
Console.WriteLine(" networkInterface id: " + nic.Id);
}
Console.WriteLine("'nvmAgent");
Console.WriteLine(" vmAgentVersion" + vmResult.InstanceView.VmAgent.VmAgentVersion);
Console.WriteLine(" statuses");
foreach (InstanceViewStatus stat in vmResult.InstanceView.VmAgent.Statuses)
{
Console.WriteLine(" code: " + stat.Code);
Console.WriteLine(" level: " + stat.Level);
Console.WriteLine(" displayStatus: " + stat.DisplayStatus);
Console.WriteLine(" message: " + stat.Message);
Console.WriteLine(" time: " + stat.Time);
}
Console.WriteLine("'ndisks");
foreach (DiskInstanceView idisk in vmResult.InstanceView.Disks)
{
Console.WriteLine(" name: " + idisk.Name);
Console.WriteLine(" statuses");
foreach (InstanceViewStatus istat in idisk.Statuses)
{
Console.WriteLine(" code: " + istat.Code);
Console.WriteLine(" level: " + istat.Level);
Console.WriteLine(" displayStatus: " + istat.DisplayStatus);
Console.WriteLine(" time: " + istat.Time);
}
}
Console.WriteLine("'nVM general status");
Console.WriteLine(" provisioningStatus: " + vmResult.ProvisioningState);
Console.WriteLine(" id: " + vmResult.Id);
Console.WriteLine(" name: " + vmResult.Name);
Console.WriteLine(" type: " + vmResult.Type);
Console.WriteLine(" location: " + vmResult.Location);
Console.WriteLine("'nVM instance status");
foreach (InstanceViewStatus istat in vmResult.InstanceView.Statuses)
{
Console.WriteLine("'n code: " + istat.Code);
Console.WriteLine(" level: " + istat.Level);
Console.WriteLine(" displayStatus: " + istat.DisplayStatus);
}
}