根据错误消息,您没有在函数应用中安装AzureAD powershell 模块。如果要创建动态组,则需要使用-MembershipRule 参数,它仅在预览版中可用,即AzureADPreview 模块。虽然doc 看起来参数在AzureAD 中可用,但根据我的测试,它不可用。
其实这个问题很容易解决,但是如果你想用New-AzureADMSGroup创建一个动态组,会有一些后续问题,你可以按照下面的步骤。
1.导航到门户中的函数应用 -> Identity -> 为您的应用启用 system-assigned identity(MSI)。
2.导航到App files -> host.json -> 确保managedDependency 是Enabled。
{
"version": "2.0",
"managedDependency": {
"Enabled": true
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
在requirements.psd1中添加AzureADPreview,如下图,它会自动为你安装AzureADPreview模块。
@{
'Az' = '5.*'
'AzureADPreview' = '2.0.2.129'
}
在profile.ps1中,删除所有的东西并添加下面几行,这是用来解决AzureAD powershell在功能上的问题,没有它会报错,详情here.
$64bitPowerShellPath = Get-ChildItem -Path $Env:Windir\WinSxS -Filter PowerShell.exe -Recurse -ErrorAction SilentlyContinue | Where-Object {$_.FullName -match "amd64"}
$env:64bitPowerShellPath=$64bitPowerShellPath.VersionInfo.FileName
3.如果你想使用New-AzureADMSGroup在Azure AD中创建组,你需要在Microsoft Graph中的权限,在这种情况下,我们使用MSI进行身份验证,所以使用下面的命令给你的MSI授予权限.
使用全局管理员用户帐户在本地运行以下命令,替换<functionapp-name>:
Connect-AzureAD
$MSI = (Get-AzureADServicePrincipal -Filter "displayName eq '<functionapp-name>'")
$MSGraphAppId = "00000003-0000-0000-c000-000000000000"
$GraphServicePrincipal = Get-AzureADServicePrincipal -Filter "appId eq '$MSGraphAppId'"
$PermissionName = "Group.ReadWrite.All"
$AppRole = $GraphServicePrincipal.AppRoles | Where-Object {$_.Value -eq $PermissionName -and $_.AllowedMemberTypes -contains "Application"}
New-AzureADServiceAppRoleAssignment -ObjectId $MSI.ObjectId -PrincipalId $MSI.ObjectId -ResourceId $GraphServicePrincipal.ObjectId -Id $AppRole.Id
4.在第2步之后,导航到kudu(在函数应用的Advanced Tools刀片中)->data->ManagedDependencies->单击格式为201208083153165.r的文件(选择通过修改时间最新的)-> 检查AzureADPreview 模块是否安装成功,如下所示。
5.模块安装后,在你的功能代码中,使用下面的行,在我的示例中,我使用这个sample直接测试,你可以根据你的要求更改代码,记得替换@987654362 @ 在第 4 步中使用你的,它在我这边运行良好。
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
}
$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) {
$body = "Hello, $name. This HTTP triggered function executed successfully."
}
$script = {
if ($env:MSI_SECRET) {
Disable-AzContextAutosave -Scope Process | Out-Null
Connect-AzAccount -Identity
}
$context = Get-AzContext
$graphtoken = (Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com").Token
$aadtoken = (Get-AzAccessToken -ResourceUrl "https://graph.windows.net").Token
Import-Module D:\home\data\ManagedDependencies\201208083153165.r\AzureADPreview
Connect-AzureAD -AccountId $context.Account -TenantId $context.Tenant -MsAccessToken $graphtoken -AadAccessToken $aadtoken
New-AzureADMSGroup -DisplayName "joyd1" -Description "Dynamic group created from PS" -MailEnabled $False -MailNickName "group" -SecurityEnabled $True -GroupTypes "DynamicMembership" -MembershipRule "(user.department -contains ""Marketing"")" -MembershipRuleProcessingState "On"
}
&$env:64bitPowerShellPath -WindowStyle Hidden -NonInteractive -Command $Script
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $body
})
在门户中查看群组: