【问题标题】:Create azure dynamic group from azure functions powershell从 azure 函数 powershell 创建 azure 动态组
【发布时间】:2021-03-18 23:37:37
【问题描述】:

我正在尝试创建一个 azure 函数,当我从 MS 流执行该函数时,该函数必须创建 azure 动态组。为此,我正在使用以下代码。

$groupName = $Request.Query.Name
$groupDesc = $Request.Query.Desc
$domainnames = $Request.Query.DomainName
$dynamicrule = ""
Foreach($domainname in $domainnames.Split(";"))
{
   $dynamicrule = $dynamicrule + "(user.userPrincipalName -contains ""_$domainname"") or";
}
$dynamicrule = $dynamicrule -replace ".{2}$"
$dynamicrule = $dynamicrule + "and (user.objectId -ne null)";
New-AzureADMSGroup -DisplayName $groupName -Description $groupDesc -MailEnabled $False -MailNickName "group" -SecurityEnabled $True -GroupTypes "DynamicMembership" -MembershipRule $dynamicrule -MembershipRuleProcessingState "On"

当我执行上述命令时,我得到以下错误消息。

错误:“New-AzureADMSGroup”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,或者如果包含路径,请确认路径正确,然后重试。异常:类型:System.Management.Automation.CommandNotFoundExceptionErrorRecord

有人可以帮我了解如何使用 azure 函数应用程序创建动态组。

谢谢, 金星

【问题讨论】:

  • 你安装了相应的PowerShell模块吗?
  • 我没有安装任何东西,因为我正在 azure 函数应用程序中安装它。如果我需要安装 azure 模块,请您指导我如何执行此操作。谢谢
  • 看看这个问题是否有帮助:stackoverflow.com/questions/57882577/…
  • 关于这个问题的任何更新?

标签: powershell azure-functions


【解决方案1】:

根据错误消息,您没有在函数应用中安装AzureAD powershell 模块。如果要创建动态组,则需要使用-MembershipRule 参数,它仅在预览版中可用,即AzureADPreview 模块。虽然doc 看起来参数在AzureAD 中可用,但根据我的测试,它不可用。

其实这个问题很容易解决,但是如果你想用New-AzureADMSGroup创建一个动态组,会有一些后续问题,你可以按照下面的步骤。

1.导航到门户中的函数应用 -> Identity -> 为您的应用启用 system-assigned identity(MSI)

2.导航到App files -> host.json -> 确保managedDependencyEnabled

{
  "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
})

在门户中查看群组:

【讨论】:

  • 请不要依赖ManagedDependencies下的文件夹名称(如201208083153165.r)。 ManagedDependencies 下的所有内容都由 Azure Functions 自动管理。如果你硬编码任何名称,它可能会工作一段时间,但 Azure Functions 最终会删除并用另一个文件夹替换此文件夹,从而破坏你的代码。这是可靠地获取模块当前位置的一种方法:(Get-Module AzureADPreview -ListAvailable).Path(从主函数体运行它,而不是从脚本块)。
猜你喜欢
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
相关资源
最近更新 更多