Loading…

READY TO ROCK?

Quick Tip: Get a list of Azure recommendations for resources

The following code can help you fetch Azure recommendations from all you subscriptions in your tenancy. I you are a CSP, the customer subscriptions should be visible under your tenancy due to the CSP delegation. If a single account has access to multiple tenancies, without having to use the CSP delegation you can also set context to all the required subscriptions along with the tenant id’s.

Note: Ensure you have Security Center aka Microsoft Defender for Cloud configured and set to install VM extension for Qualys by enabling the defender plan for required resources.

$cspTenant = "<tenant ID>"
Connect-AzAccount -Tenant $cspTenant
$outfile = "c:\temp\recommendations.csv"
$Subscriptions = Get-azSubscription
$recommendations = $null
foreach($subscription in $subscriptions){
    $null = Set-azContext $subscription.Id
    $context = Get-azContext
    Write-Host ("Working with subscription '{0}' having id '{1}' in tenancy '{2}' " -f $context.Subscription.Name, $context.Subscription.id, $context.Subscription.tenantid)
    $recommendations += Get-AzAdvisorRecommendation | Select-Object *, @{l="SubscriptionID"; e= {$subscription.id}}, @{l="TenantID"; e= {$subscription.TenantId}}, @{l="HomeTenantID"; e= {$subscription.HomeTenantId}}, @{l="SubscriptionName"; e= {$subscription.Name}}, @{l="ManagedbyTenantID"; e= {$subscription.ManagedByTenantIds[0]}}

}

$finalRecommendations = $recommendations | Select-Object ResourceId,Category,Impact,ImpactedField,ImpactedValue,LastUpdated,Risk,Name,Type,SubscriptionID,TenantID,HomeTenantID,SubscriptionName,ManagedbyTenantID, @{l="Resource Type"; e= {$_.ExtendedProperties.reservedResourceType}}, `
                        @{l="Scope"; e= {$_.ExtendedProperties.Scope}}, `
                        @{l="SKU"; e= {$_.ExtendedProperties.SKU}}, `
                        @{l="Term"; e= {$_.ExtendedProperties.term}}, `
                        @{l="Annual Savings Amount"; e= {$_.ExtendedProperties.annualSavingsAmount}}, `
                        @{l="Savings Amount"; e= {$_.ExtendedProperties.savingsAmount}}, `
                        @{l="Quantity"; e= {$_.ExtendedProperties.savingsAmount}}, ` 
                        @{l="Savings Currency"; e= {$_.ExtendedProperties.savingsCurrency}}, `
                        @{l="Region"; e= {$_.ExtendedProperties.region}}, `
                        @{l="Display SKU"; e= {$_.ExtendedProperties.DisplaySKU}}, `
                        @{l="Recommendation"; e= {$_.ShortDescription.Problem}}, `
                        @{l="Solution"; e= {$_.ShortDescription.Solution}}

$finalRecommendations | Export-CSV -NoTypeInformation -Path $outfile

Invoke-Item $outfile