How to get Azure VM operating sytsem detail version by Powershell

Mike Zhang 1 Reputation point
2021-08-05T09:21:10.387+00:00

Because our vender provides customized image, so the below Get-AzVM can't get OS version.
$VM = Get-AzVM -Name $VMName
$VM.StorageProfile.ImageReference

I find the below way for Linux,
$CommandId = 'RunShellScript'
$ScriptPath = ".\RemoteCommand\Get-RHELVersion.sh"
$Result = Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId $CommandId -ScriptPath $ScriptPath -ErrorAction Stop

$Result | ConvertTo-Json
$OSMessage = $Result.Value[0].Message
$StartPos = $OSMessage.IndexOf("Red Hat Enterprise Linux Server") + 32
$OSLength = $OSMessage.IndexOf("`n CPE OS Name") - $StartPos
$Version = $OSMessage.Substring($StartPos, 3)

Get-RHELVersion.sh contains only one command: hostnamectl
Is the above OK?
I have big concern that the RunCommand will impact the VM, that means bring huge risk to Production VM.
Any better solution?

Azure VMware Solution
Windows for business | Windows Server | User experience | Other
{count} votes

2 answers

Sort by: Most helpful
  1. SUNOJ KUMAR YELURU 16,686 Reputation points MVP Volunteer Moderator
    2021-08-05T16:29:06.53+00:00

    Hi @Mike Zhang

    Try with the below script to get OS type.

    $reportName = "myReport.csv"  
      
    $report = @()  
    $vms = Get-AzVM  
    $publicIps = Get-AzPublicIpAddress   
    $nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}   
    foreach ($nic in $nics) {   
        $info = "" | Select VmName, ResourceGroupName, Region, VirturalNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress   
        $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id   
        foreach($publicIp in $publicIps) {   
            if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {  
                $info.PublicIPAddress = $publicIp.ipaddress  
                }   
            }   
            $info.OsType = $vm.StorageProfile.OsDisk.OsType   
            $info.VMName = $vm.Name   
            $info.ResourceGroupName = $vm.ResourceGroupName   
            $info.Region = $vm.Location   
            $info.VirturalNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3]   
            $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1]   
            $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress   
            $report+=$info   
        }   
    $report | ft VmName, ResourceGroupName, Region, VirturalNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress   
    $report | Export-CSV "$home/$reportName"  
    

    If the Answer is helpful, please click Accept Answer and up-vote, this can be beneficial to other community members.

    0 comments No comments

  2. André Sikma 45 Reputation points
    2025-06-06T07:34:53.3133333+00:00

    Hi,

    If the machine is running, add switch -status to the call to Get-AzVM. Then the properties OSName and OSVersion are populated with information of the current OS. The information reflected in the StorageProfile contains the OS of the initial deployment. If an in-place upgrade or windows Updates have been performed, the StorageProfile does not reflect this, where OSName and more specifically OSVersion will reflect this.

    $vms = Get-AzVM -Status
    

    Sample of results

    OS Name OS Version
    ubuntu 20.04
    ubuntu 24.04
    Windows Server 2016 Datacenter 10.0.14393.8066
    Windows Server 2019 Datacenter 10.0.17763.7314
    Windows Server 2022 Datacenter 10.0.20348.3692
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.