Azure 函数无法识别 Get-AzADApplication

标签 azure azure-functions azure-powershell

我创建了一个 azure 函数,我试图在其中执行代码Get-AzADApplication -DisplayName cs-sp-aro-testrotation

当我运行时,我收到错误如下 术语“Get-AzADApplication”未被识别为 cmdlet 的名称

我正在粘贴我的整个 azure 函数代码,请告诉我如何做同样的事情

 using namespace System.Net
   
    # Input bindings are passed in via param block.
    param($Request, $TriggerMetadata)
    
    # Write to the Azure Functions log stream.
    Write-Host "PowerShell HTTP trigger function processed a request."
    
    # Interact with query parameters or the body of the request.
    $name = $Request.Query.Name
    if (-not $name) {
        $name = $Request.Body.Name
    }
    Get-AzADApplication -DisplayName cs-sp-aro-testrotation
    $body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
    
    if ($name) {
        Get-AzADApplication -DisplayName cs-sp-aro-testrotation
        $body = "Hello, $name. This HTTP triggered function executed successfully."
    }
    
    # Associate values to output bindings by calling 'Push-OutputBinding'.
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        StatusCode = [HttpStatusCode]::OK
        Body = $body
    })

最佳答案

导航到门户中的函数应用 -> App files -> requirements.psd1,确保您已安装 Az powershell module(对于新创建的应用程序,此行默认会被注释,如果有,只需取消注释即可)。

@{ 
    'Az' = '5.*'
}

enter image description here

更新:

当然需要登录,默认会使用MSI(managed identity)登录自动显示您的函数应用程序,如 profile.ps1 中所示。

if ($env:MSI_SECRET) {
    Disable-AzContextAutosave -Scope Process | Out-Null
    Connect-AzAccount -Identity
}

为了让 Get-AzADApplication 正常工作,您还需要执行其他步骤。

1.导航至门户中的Identity -> 启用系统分配的 MSI,如下所示。

enter image description here

2.导航到门户中的Azure Active Directory -> 角色和管理员 -> 搜索目录读取器 -> 单击它-> 添加分配 -> 将 MSI 添加为目录读取器 角色(只需搜索您的函数应用名称)。

enter image description here

然后运行Get-AzADApplication -DisplayName xxxx,它将正常工作。

enter image description here

关于Azure 函数无法识别 Get-AzADApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67208333/

相关文章:

azure - Powershell - 删除容器中的所有 Blob

powershell - ParseExact 和 TryParseExact 方法不接受 powershell 脚本中的多种日期格式

azure - 无法删除或创建 azure aks 实例

azure - 在 Microsoft Azure 应用服务上部署 Next.js 应用

azure - 应在 Azure Function 应用程序中的何处设置 ServicePointManager.DefaultConnectionLimit?

Azure Function 作为 Web API 性能和定价

azure - 如何安装 Azure Az - 无法将值 "2.0.0-preview"转换为类型 "System.Version"

azure - 将 Application Insights 作为 Azure 应用服务扩展安装还是通过 NuGet 安装?

Azure函数应用程序Http重定向

azure-powershell - PowerShell 7. ForEach-Object -Parallel 不会针对 Azure PowerShell 进行身份验证