locked
Azure VM - OS build number RRS feed

  • Question

  • Hello Everyone,

    I would like to loop through all the VM's and find the OS build number. I found ways post logging into the VM like msinfo32, [System.Environment]::OSversion etc but none on the lines of remotely getting the info using powershell.

    With the limited knowledge I created the below script but I am not getting the OSbuild number :(

    param(
        # Specify the location of the audit file
        $csvFilePath = "C:\agentAudit.csv"
    )
    cls
    
    $ErrorActionPreference = 'Stop'
    Write-Host "Validating Azure Accounts..."
    try{
        $subscriptionList = Get-AzureRmSubscription | Sort SubscriptionName
    }
    catch {
        Write-Host "Reauthenticating..."
        Login-AzureRmAccount | Out-Null
        $subscriptionList = Get-AzureRmSubscription | Sort SubscriptionName
    }
    if (Test-Path $csvFilePath) {
        Remove-Item -Path $csvFilePath
    }
    foreach($subscription in $subscriptionList) {
        Select-AzureRmSubscription -SubscriptionId $subscription.SubscriptionId | Out-Null
        Write-Output "`n Working on subscription: $($subscription.SubscriptionName) `n"
        $vms = Get-AzureRmVM -WarningAction Ignore
        foreach ($vm in $vms) {
        $VMs = Get-AzureRmVM 
        $vmlist = @() 
        $VMs | ForEach-Object {  
            $VMObj = New-Object -TypeName PSObject 
            $VMObj | Add-Member -MemberType Noteproperty -Name "VM Name" -Value $_.Name 
            $VMObj | Add-Member -MemberType Noteproperty -Name "OS type" -Value $_.StorageProfile.ImageReference.Sku
            $VMObj | Add-Member -MemberType Noteproperty -Name "OS Offer" -Value $_.StorageProfile.ImageReference.Offer
            $VMObj | Add-Member -MemberType Noteproperty -Name "OS Publisher" -Value $_.StorageProfile.ImageReference.Publisher
            $VMObj | Add-Member -MemberType Noteproperty -Name "OS Version" -Value $_.StorageProfile.ImageReference.Version
        $vmlist += $VMObj 
    } 
    $vmlist  
    }
    }


    I would appreciate if anyone with PS scripting knowledge would be able to help me

    • Moved by Bill_Stewart Wednesday, December 12, 2018 5:12 PM This is not "debug/fix/rewrite my script for me" forum
    Wednesday, August 15, 2018 6:05 PM

All replies

  •  Get-WmiObject win32_operatingsystem -Computer MyVM1| select Caption,Manufacturer,version,buildnumber

    \_(ツ)_/

    Wednesday, August 15, 2018 6:33 PM
  • Hey Thanks for replying but I am getting an error

    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)At line:1 char:1+ Get-WmiObject win32_operatingsystem -Computer bhminvm| select Caption ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    am I doing something wrong

    Wednesday, August 15, 2018 6:55 PM
  • Firewall?   Is the VM a non-Windows VM?

    \_(ツ)_/

    Wednesday, August 15, 2018 6:58 PM
  • Its a windows VM and no additional firewall/security apart from the default present in the OS.
    Wednesday, August 15, 2018 7:01 PM
  • If it is not a part of the network you are running from you will need to use credentials.

    Note that all Windows Version numbers contain the buildnumber in the last digits.

    ([version]$version).Build

    or

    ([version]$_.StorageProfile.ImageReference.Version).Build


    \_(ツ)_/


    • Edited by jrv Wednesday, August 15, 2018 7:05 PM
    Wednesday, August 15, 2018 7:04 PM
  • When I add these parameters to my script I am getting the following error

     $VMs | ForEach-Object {          $VMObj = New-Object -TypeName PSObject         $VMObj | Add-Member -MemberType Noteproperty -Name "VM Name" -Value $_.Name         $VMObj | Add-Member -MemberType Noteproperty -Name "OS type" -Value $_.StorageProfile.ImageReference.Sku        $VMObj | Add-Member -MemberType Noteproperty -Name "OS Offer" -([version]$_.StorageProfile.ImageReference).Build        $VMObj | Add-Member -MemberType Noteproperty -Name "OS Publisher" -Value $_.StorageProfile.ImageReference.Publisher        $VMObj | Add-Member -MemberType Noteproperty -Name "OS Version" -Value $_.StorageProfile.ImageReference.Version    $vmlist += $VMObj 

    ForEach-Object : Cannot convert the 

    "Microsoft.Azure.Management.Compute.Models.ImageReference" value of type 
    "Microsoft.Azure.Management.Compute.Models.ImageReference" to type "System.Version".
    At line:27 char:12
    +     $VMs | ForEach-Object {
    +            ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], RuntimeException
        + FullyQualifiedErrorId : ConvertToFinalInvalidCastException,Microsoft.PowerShell.Co 
       mmands.ForEachObjectCommand

    Wednesday, August 15, 2018 7:21 PM
  • The version number you are using is not a version number and is not an OS version.  I didn't test it and did not expect it to be a non-standard version.

    You must use the WMI call to get the info.  You will have to troubleshoot the connections and firewall setting.

    You cannot use WMI across the Internet.  If you have remoting enabled and configured than use that.


    \_(ツ)_/

    Wednesday, August 15, 2018 7:27 PM