How to retrieve the OS of the Azure VM using Azure CLI in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To retrieve the Azure VM OS using Azure CLI, we can use the “az vm” command but before that, need to make sure that you are connected to the Azure cloud and the subscription.

PS C:\> az vm show -n VMName -g VMRG --query
"[storageProfile.imageReference.offer]" -otsv

OR

PS C:\> az vm show -n VMName -g VMRG --query storageProfile.imageReference.offer -
otsv

Output

WindowsServer

To get the OS SKU or the operating system version, you can use,

PS C:\> az vm show -n VMName -g VMRG --query
"[storageProfile.imageReference.sku]" -otsv

Output

2016-Datacenter

You can also use the below command to get the OS of the VM without providing the resource group name.

PS C:\> az vm list --query
"[?name==VMName].{os:storageProfile.imageReference.offer}" -otsv

To get the OS version or SKU,

PS C:\> az vm list --query "[?name==VMName].{os:storageProfile.imageReference.sku}"
-otsv

To combine all the outputs in the table format,

az vm list --query "[].{vmName:name, ResourceGroup:resourceGroup,
os:storageProfile.imageReference.offer, version:storageProfile.imageReference.sku}" -
otable
raja
Published on 02-Sep-2021 10:56:27
Advertisements