Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.21k stars 3.82k forks source link

`Get-AzWebApp` on subscription is very slow #24983

Open o-l-a-v opened 3 months ago

o-l-a-v commented 3 months ago

Description

Get-AzWebApp with no further parameters for getting all web apps on a subscription is amazingly slow. Might be related to #18266.

It seems to first get resource groups, then foreach those one by one to look for web apps.

Why not use the API directly to LIST all web apps on a subscription in this case?

Here's an example for getting all web apps much faster:

# Assets
$ResourceType = [string] 'Microsoft.Web/sites'

# Get resources as fast as possibru
## Prepare
$Subscriptions = [PSCustomObject[]](
    (Search-AzGraph -UseTenantScope -Query ('resources | where type =~ "{0}" | summarize count() by subscriptionId | project subscriptionId' -f $ResourceType.ToLower())).'Data'
)
$Headers = [ordered]@{
    'authorization' = [string] 'Bearer {0}' -f (Get-AzAccessToken -ResourceTypeName 'Arm').'Token'
}
$ApiVersion = [string](
    (
        Invoke-RestMethod -Method 'Get' -Headers $Headers -Uri (
            'https://management.azure.com/subscriptions/{0}/providers/{1}?api-version=2015-01-01' -f $Subscriptions[0].'subscriptionId', $ResourceType.Split('/')[0]
        ) -SessionVariable 'Session'
    ).'resourceTypes'.Where(
        {$_.'resourceType' -eq ($ResourceType -replace ([string]::Format('^{0}/',$ResourceType.Split('/')[0])),'')},'First'
    ).'apiVersions'.Where{
        -not $_.EndsWith('-preview','InvariantCultureIgnoreCase')
    } | Sort-Object -Descending | Select-Object -First 1
)

## Get
$Resources = [PSCustomObject[]](
    $Subscriptions.'subscriptionId' | ForEach-Object -ThrottleLimit 10 -Parallel {
        [PSCustomObject[]](
            $(
                Invoke-RestMethod -Uri (
                    'https://management.azure.com/subscriptions/{0}/providers/{1}?api-version={2}' -f $_, $Using:ResourceType, $Using:ApiVersion
                ) -Headers $Using:Headers -WebSession $Using:Session
            ).'value'
        )
    }
)

Issue script & Debug output

Get-AzWebApp # Watch painting dry

Environment data

PowerShell v7.4.2 x64 on Windows 11 23H2

Module versions

Az.Websites v3.2.1

Error output

No error.
microsoft-github-policy-service[bot] commented 3 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @antcp, @AzureAppServiceCLI.

microsoft-github-policy-service[bot] commented 3 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @antcp, @AzureAppServiceCLI.