Advertisement
Guest User

Untitled

a guest
May 30th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. using System;
  2. using System.Security;
  3. using Microsoft.Azure.Management.Compute;
  4. using Microsoft.Azure.Management.Compute.Models;
  5. using Microsoft.IdentityModel.Clients.ActiveDirectory;
  6. using Microsoft.Rest;
  7.  
  8. namespace GetVmARM
  9. {
  10. class Program
  11. {
  12.  
  13. private static String tenantID = "<your tenant id>";
  14. private static String loginEndpoint = "https://login.windows.net/";
  15. private static Uri redirectURI = new Uri("urn:ietf:wg:oauth:2.0:oob");
  16. private static String clientID = "1950a258-227b-4e31-a9cf-717495945fc2";
  17. private static String subscriptionID = "<your subscription id>";
  18. private static String resource = "https://management.core.windows.net/";
  19.  
  20.  
  21.  
  22. static void Main(string[] args)
  23. {
  24. var token = GetTokenCloudCredentials();
  25. var credential = new TokenCredentials(token);
  26. var computeManagementClient = new ComputeManagementClient(credential);
  27. computeManagementClient.SubscriptionId = subscriptionID;
  28.  
  29. InstanceViewTypes expand = new InstanceViewTypes();
  30.  
  31. var vm = computeManagementClient.VirtualMachines.Get("<the resource group name>", "<the VM>", expand);
  32.  
  33. System.Console.WriteLine(vm.InstanceView.Statuses[1].Code);
  34. System.Console.WriteLine("Press ENTER to continue");
  35. System.Console.ReadLine();
  36. }
  37.  
  38. public static String GetTokenCloudCredentials(string username = null, SecureString password = null)
  39. {
  40. String authString = loginEndpoint + tenantID;
  41.  
  42. AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
  43.  
  44. var promptBehaviour = PromptBehavior.Auto;
  45.  
  46. var userIdentifierType = UserIdentifierType.RequiredDisplayableId;
  47.  
  48. var userIdentifier = new UserIdentifier("<your azure account>", userIdentifierType);
  49.  
  50. var authenticationResult = authenticationContext.AcquireToken(resource, clientID, redirectURI, promptBehaviour, userIdentifier);
  51.  
  52. return authenticationResult.AccessToken;
  53. }
  54.  
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement