Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save roberto-mardeni/f043e5de86cd3a5d2b4f2e62b542b0cf to your computer and use it in GitHub Desktop.
Save roberto-mardeni/f043e5de86cd3a5d2b4f2e62b542b0cf to your computer and use it in GitHub Desktop.
Get Azure Virtual Machine Status using Azure .Net SDK - CSHARP
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Compute;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Compute.Models;
// Code omitted for brevity
ArmClient client = new ArmClient(new DefaultAzureCredential());
string resourceGroupName = "vsonvm";
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();
ResourceGroupResource resourceGroup = await resourceGroups.GetAsync(resourceGroupName);
await foreach (VirtualMachineResource virtualMachine in resourceGroup.GetVirtualMachines())
{
//previously we would have to take the resourceGroupName and the vmName from the vm object
//and pass those into the powerOff method as well as we would need to execute that on a separate compute client
VirtualMachineInstanceView vmIV = await virtualMachine.InstanceViewAsync();
Console.WriteLine($"{vmIV.ComputerName} statuses:");
foreach (InstanceViewStatus status in vmIV.Statuses)
{
Console.WriteLine($"{status.Code} - {status.DisplayStatus}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment