Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. var groupName = "XYZ";
  4. var vmName = "DC1";
  5. var location = "Southeast Asia";
  6. var subscriptionId = "My Subscription ID";
  7.  
  8. var token = GetAccessTokenAsync();
  9. var credential = new TokenCredentials(token.Result.AccessToken);
  10.  
  11. GetVirtualMachineAsync( credential, groupName, vmName, subscriptionId);
  12. }
  13.  
  14. private static async Task<AuthenticationResult> GetAccessTokenAsync()
  15. {
  16. var cc = new ClientCredential("{client-id}", "{client-secret}");
  17. var context = new AuthenticationContext("https://login.windows.net/{tenant-id}");
  18. var result = await context.AcquireTokenAsync("https://management.azure.com/", cc);
  19. if (result == null)
  20. {
  21. throw new InvalidOperationException("Could not get the token");
  22. }
  23. return result;
  24. }
  25. public static async void GetVirtualMachineAsync( TokenCredentials credential, string groupName, string vmName string subscriptionId)
  26. {
  27. Console.WriteLine("Getting information about the virtual machine...");
  28.  
  29. var computeManagementClient = new ComputeManagementClient(credential)
  30. { SubscriptionId = subscriptionId };
  31. var vmResult = await computeManagementClient.VirtualMachines.GetAsync(
  32. groupName,
  33. vmName,
  34. InstanceViewTypes.InstanceView);
  35.  
  36. Console.WriteLine("hardwareProfile");
  37. Console.WriteLine(" vmSize: " + vmResult.HardwareProfile.VmSize);
  38.  
  39. Console.WriteLine("nstorageProfile");
  40. Console.WriteLine(" imageReference");
  41. Console.WriteLine(" publisher: " + vmResult.StorageProfile.ImageReference.Publisher);
  42. Console.WriteLine(" offer: " + vmResult.StorageProfile.ImageReference.Offer);
  43. Console.WriteLine(" sku: " + vmResult.StorageProfile.ImageReference.Sku);
  44. Console.WriteLine(" version: " + vmResult.StorageProfile.ImageReference.Version);
  45. Console.WriteLine(" osDisk");
  46. Console.WriteLine(" osType: " + vmResult.StorageProfile.OsDisk.OsType);
  47. Console.WriteLine(" name: " + vmResult.StorageProfile.OsDisk.Name);
  48. Console.WriteLine(" createOption: " + vmResult.StorageProfile.OsDisk.CreateOption);
  49. Console.WriteLine(" uri: " + vmResult.StorageProfile.OsDisk.Vhd.Uri);
  50. Console.WriteLine(" caching: " + vmResult.StorageProfile.OsDisk.Caching);
  51.  
  52. Console.WriteLine("nosProfile");
  53. Console.WriteLine(" computerName: " + vmResult.OsProfile.ComputerName);
  54. Console.WriteLine(" adminUsername: " + vmResult.OsProfile.AdminUsername);
  55. Console.WriteLine(" provisionVMAgent: " + vmResult.OsProfile.WindowsConfiguration.ProvisionVMAgent.Value);
  56. Console.WriteLine(" enableAutomaticUpdates: " + vmResult.OsProfile.WindowsConfiguration.EnableAutomaticUpdates.Value);
  57.  
  58. Console.WriteLine("nnetworkProfile");
  59. foreach (NetworkInterfaceReference nic in vmResult.NetworkProfile.NetworkInterfaces)
  60. {
  61. Console.WriteLine(" networkInterface id: " + nic.Id);
  62. }
  63.  
  64. Console.WriteLine("nvmAgent");
  65. Console.WriteLine(" vmAgentVersion" + vmResult.InstanceView.VmAgent.VmAgentVersion);
  66. Console.WriteLine(" statuses");
  67. foreach (InstanceViewStatus stat in vmResult.InstanceView.VmAgent.Statuses)
  68. {
  69. Console.WriteLine(" code: " + stat.Code);
  70. Console.WriteLine(" level: " + stat.Level);
  71. Console.WriteLine(" displayStatus: " + stat.DisplayStatus);
  72. Console.WriteLine(" message: " + stat.Message);
  73. Console.WriteLine(" time: " + stat.Time);
  74. }
  75.  
  76. Console.WriteLine("ndisks");
  77. foreach (DiskInstanceView idisk in vmResult.InstanceView.Disks)
  78. {
  79. Console.WriteLine(" name: " + idisk.Name);
  80. Console.WriteLine(" statuses");
  81. foreach (InstanceViewStatus istat in idisk.Statuses)
  82. {
  83. Console.WriteLine(" code: " + istat.Code);
  84. Console.WriteLine(" level: " + istat.Level);
  85. Console.WriteLine(" displayStatus: " + istat.DisplayStatus);
  86. Console.WriteLine(" time: " + istat.Time);
  87. }
  88. }
  89.  
  90. Console.WriteLine("nVM general status");
  91. Console.WriteLine(" provisioningStatus: " + vmResult.ProvisioningState);
  92. Console.WriteLine(" id: " + vmResult.Id);
  93. Console.WriteLine(" name: " + vmResult.Name);
  94. Console.WriteLine(" type: " + vmResult.Type);
  95. Console.WriteLine(" location: " + vmResult.Location);
  96. Console.WriteLine("nVM instance status");
  97. foreach (InstanceViewStatus istat in vmResult.InstanceView.Statuses)
  98. {
  99. Console.WriteLine("n code: " + istat.Code);
  100. Console.WriteLine(" level: " + istat.Level);
  101. Console.WriteLine(" displayStatus: " + istat.DisplayStatus);
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement