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
Azure VMware Solution
An Azure service that runs native VMware workloads on Azure.
385 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,726 questions
{count} votes

1 answer

Sort by: Most helpful
  1. SUNOJ KUMAR YELURU 15,011 Reputation points MVP
    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

Your answer

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