Dmitry Porotnikov / PowerShell: Check AzureVM license type (Hybrid Benefit)

Created Tue, 22 Aug 2023 13:35:06 +0000 Modified Tue, 22 Aug 2023 13:35:06 +0000
116 Words

PowerShell: Check AzureVM license type (Hybrid Benefit)

# Login to Azure
Connect-AzAccount

# Set the subscription context
$subscriptionId = "<your subscription id>"
Set-AzContext -SubscriptionId $subscriptionId

# Get all Linux VMs in the subscription
$vms = Get-AzVM | Where-Object { $_.StorageProfile.OSDisk.OSType -eq "Linux" }

# Loop through each VM and check the license type (You should see *-BYOS, e.g rhel-byos in case of BYOS)
foreach ($vm in $vms) {

    $vmConfig = Get-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
    Write-Host "VM $($vm.Name) publisher: $($vmConfig.StorageProfile.ImageReference.Publisher)"
    Write-Host "VM $($vm.Name) offer: $($vmConfig.StorageProfile.ImageReference.Offer)"

    #checking for HybridBenefit (https://learn.microsoft.com/en-us/azure/virtual-machines/linux/azure-hybrid-benefit-linux)
    if ($vmConfig.LicenseType -eq $null) {
        Write-Host "VM $($vm.Name) has no hybrid benefit enabled"
    }
    else {
        Write-Host "VM $($vm.Name) has hybrid benefit enabled"
    }
    Write-Host "------""
    ```