New-AzureADUserAppRoleAssignment扩展

时间:2018-07-13 20:51:56

标签: powershell azure azure-active-directory cmdlets

我正在创建一个用户,并使用此cmdlet应用一个应用程序角色,该角色位于Azure内置的自定义应用程序中,并且运行良好。关键是,我有多个Azure自定义构建的应用程序,它们具有希望用户在每个应用程序中拥有的相同角色。有没有一种使用此cmdlet的方法,比如说,将-ResourceId设置为多个ServicePrincipal,以允许该用户一次在每个应用程序中提取此用户,或者我必须一个一个地做到这一点?

我当前的代码:

$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = "<Password>"
$PasswordProfile.EnforceChangePasswordPolicy = $True
New-AzureADUser -DisplayName "<Display Name of User>" -PasswordProfile $PasswordProfile -UserPrincipalName "<developer's test user>@dosinvest.onmicrosoft.com" -AccountEnabled $true -MailNickName "<NickName to use for Mail name>"
$username = "<developer's test user>@dosinvest.onmicrosoft.com"
$app_name = "<Name of Application>"
$app_role_name = "<Display Name of Role within Application>"
$user = Get-AzureADUser -ObjectId "$username"
$sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
$appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id

1 个答案:

答案 0 :(得分:0)

ResourceID参数基于its documentation接受String作为输入,因此无法为其赋予多个ID。

ForEach可能会帮助您。例如,让我们定义应用程序名称的数组(注意,用 names 代替name):

$app_names = "<Name of Application>","<Name of Application2>"

您基本上需要迭代此数组,并从列表中为每个应用程序运行cmdlet(假设$app_role_name对于所有应用程序都是相同的):

$user = Get-AzureADUser -ObjectId "$username"
$app_role_name = "<Display Name of Role within Application>"

foreach ($n in $app_names) {
  $sp = Get-AzureADServicePrincipal -Filter "displayName eq '$n'"
  $appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
  New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id
}

其他资源: