Home > Blockchain >  Powershell - how to read Azure VM status and a certain tag value?
Powershell - how to read Azure VM status and a certain tag value?

Time:12-24

I need to have a report of VM status and the value of a specific tag. The VM status part I can handle, but I do not know how add the TAG part into the same script.

Get-Azvm -Status | 
    Select-Object Name,ResourceGroupName,PowerState | 
        Where-Object Name -in "SOBA-VM1","SO-VM2","debian01"

Please help me.

CodePudding user response:

If you are able to switch to using Azure CLI instead of AzPowershell (which I would recommend any day), then here you go:

az vm get-instance-view --name MY-VM --resource-group MY-RG --query "{name:name, resourceGroup:resourceGroup, state:instanceView.statuses[1].code, tags:tags}" | ConvertFrom-Json

enter image description here

Also, here the version with PowerShell

 Get-Azvm -Status | where-object {$_.Name -in "SOBA-VM1","SO-VM2","debian01"} | select Name,Tags,PowerState,ResourceGroupName

CodePudding user response:

So, in the end, to read a certain Tag value for a VM list, I used the below script:

Get-Azvm -Status | 
    Select-Object Name,ResourceGroupName,PowerState,@{Name="running_enddate";Expression={ $_.Tags["running_enddate"] }},@{Name="running_profile";Expression={ $_.Tags["running_profile"] }} | 
        Where-Object {$_.Name -in "VM01","VM02"} | Format-List
  • Related