Reputation: 838
So I've been poking around at read-only api access into azure with the resource management api. Right now I'm focusing on Virtual Machines. I've been using this pre-release package with TokenCredentials:
https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.1-prerelease
I get a bunch of rich info about my vms but I'm missing a pretty criticle piece of data and that's whether the vm is on or off. I've found a couple of meta data properties like InstanceView and Plan to be null when I expected them to be populated. It may because of how I launched my vms, it may be a unfinished or buggy new package, I can't tell. I was thinking InstanceViews statues would tell me what state the vm is in.
So I suppose I have to look elsewhere. I did find this older stackoverflow question that may be what I'm looking for:
azure management libraries virtual machine state
However I'm not sure what dll this GetAzureDeyployment is part of or if it's even TokenCredential compatible. Anyone know whats up?
Upvotes: 1
Views: 841
Reputation: 2267
You can use the following c# code to get the power status of your VM.
using System;
using System.Security;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
namespace GetVmARM
{
class Program
{
private static String tenantID = "<your tenant id>";
private static String loginEndpoint = "https://login.windows.net/";
private static Uri redirectURI = new Uri("urn:ietf:wg:oauth:2.0:oob");
private static String clientID = "1950a258-227b-4e31-a9cf-717495945fc2";
private static String subscriptionID = "<your subscription id>";
private static String resource = "https://management.core.windows.net/";
static void Main(string[] args)
{
var token = GetTokenCloudCredentials();
var credential = new TokenCredentials(token);
var computeManagementClient = new ComputeManagementClient(credential);
computeManagementClient.SubscriptionId = subscriptionID;
InstanceViewTypes expand = new InstanceViewTypes();
var vm = computeManagementClient.VirtualMachines.Get("<the resource group name>", "<the VM>", expand);
System.Console.WriteLine(vm.InstanceView.Statuses[1].Code);
System.Console.WriteLine("Press ENTER to continue");
System.Console.ReadLine();
}
public static String GetTokenCloudCredentials(string username = null, SecureString password = null)
{
String authString = loginEndpoint + tenantID;
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
var promptBehaviour = PromptBehavior.Auto;
var userIdentifierType = UserIdentifierType.RequiredDisplayableId;
var userIdentifier = new UserIdentifier("<your azure account>", userIdentifierType);
var authenticationResult = authenticationContext.AcquireToken(resource, clientID, redirectURI, promptBehaviour, userIdentifier);
return authenticationResult.AccessToken;
}
}
}
As you can see in this piece of code, I am using InstanceViewTypes which is not available in the document. This is new in the 13.0.1 pre-release version. But yes, if you adding this to your computeManagementClient.VirtualMachines.Get method, you will be able to get extra information for your VM.
Furthermore, I am using vm.InstanceView.Statuses[1] because vm.InstanceView.Statuses[0] is the ProvisioningState. And, I am not sure if the order is always like this, so you may need to loop through the whole status list.
Upvotes: 2