Created
July 14, 2022 14:48
-
-
Save roberto-mardeni/f043e5de86cd3a5d2b4f2e62b542b0cf to your computer and use it in GitHub Desktop.
Get Azure Virtual Machine Status using Azure .Net SDK - CSHARP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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