【问题标题】:Get total number of Azure functions in a subscription获取订阅中的 Azure 函数总数
【发布时间】:2022-09-27 15:29:40
【问题描述】:

我正在尝试使用 Azure Resource Graph Explorer 查找查询以列出订阅中的 Azure 函数总数。

我只能获得功能应用程序的总数,而不是底层功能。

有没有办法使用 Graph 查询?或者有我可以使用的脚本吗?

注意:只对 Azure Functions 感兴趣,而不是 Azure Function App。

    标签: azure azure-functions azure-resource-graph


    【解决方案1】:

    我认为这不可能通过资源图查询来实现。一种可能的方法是随后使用 Azure REST API 来获取结果。这是一个 PowerShell 示例:

    您需要生成 Bearer 令牌才能查询 REST API。您可以使用这样的函数来生成它。

    function Get-AzOauth2Token
    {
        [CmdletBinding()]
        Param
        (
            [string]$TenantId,
            [string]$AppId,
            [string]$Secret
        )
    
        $result         =   Invoke-RestMethod -Uri $('https://login.microsoftonline.com/'+$TenantId+'/oauth2/token?api-version=1.0') -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "$AppId"; "client_secret" = "$Secret" }
        $authorization  =   ("{0} {1}" -f $result.token_type, $result.access_token)
        return $authorization
    }
    

    不过,还有很多其他方法可以获得令牌。但是,我将使用它来检索它...

    $token = Get-AzOauth2Token -TenantId your_tenant -AppId your_spn_app_id -Secret your_secret
    

    然后,您将运行资源图查询,以获取跨租户和任何订阅的所有函数应用程序。

    $query = Search-AzGraph "resources | where type =~ 'microsoft.web/sites' | where kind startswith 'functionapp'"
    $results = Search-AzGraph -Query $query
    

    ...最后为查询返回的所有函数应用程序执行 REST API 调用。

    $functions = @()
    
    $results | ForEach-Object {
    
        $restMethod = 'GET'
        $restUri    = 'https://management.azure.com'+$_.ResourceId+'/functions?api-version=2022-03-01'
        $restHeader = @{
            'Authorization' = $token
            'Content-Type'  = 'application/json'
        }
    
        # Execute Call
        $request    = Invoke-RestMethod -Method $restMethod `
                                        -Uri $restUri `
                                        -Headers $restHeader
    
        $functions += $request
    
    }
    

    $functions.value 变量现在包含所有不同的功能。

    我建议使用 REST API 而不是标准的 PowerShell cmdlet,因为它在大型环境中速度更快 - 当您的资源分布在各种订阅中时,它可以防止您在订阅之间切换。

    【讨论】:

    • 谢谢你。我认为上面的资源图查询没有列出我订阅的所有功能应用程序。不应该吗?资源 |其中类型 =~ 'microsoft.web/serverfarms' |种类 =~ 'functionapp'
    • 哦,发现了错误。资源图查询不包括在 Linux 计划上运行的函数应用。编辑了帖子。这应该会更好:resources | where type =~ 'microsoft.web/sites' | where kind startswith 'functionapp'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多