我创建了一个 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.*'
}
更新:
当然需要登录,默认会使用MSI(managed identity)登录自动显示您的函数应用程序,如 profile.ps1
中所示。
if ($env:MSI_SECRET) {
Disable-AzContextAutosave -Scope Process | Out-Null
Connect-AzAccount -Identity
}
为了让 Get-AzADApplication
正常工作,您还需要执行其他步骤。
1.导航至门户中的Identity
-> 启用系统分配的 MSI,如下所示。
2.导航到门户中的Azure Active Directory
-> 角色和管理员
-> 搜索目录读取器
-> 单击它-> 添加分配
-> 将 MSI 添加为目录读取器
角色(只需搜索您的函数应用名称)。
然后运行Get-AzADApplication -DisplayName xxxx
,它将正常工作。
关于Azure 函数无法识别 Get-AzADApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67208333/