LoginSignup
3
0

More than 3 years have passed since last update.

Azure AD に追加したアプリに PowerShell でユーザーやグループを割り当てる

Posted at

Azure AD の GUI ではアプリへのユーザー割り当てが難しいシーン

アプリにグループを割り当てるには有償ライセンスが必要

Azure AD でシングルサインオン等を行うために追加した SaaS アプリケーションにユーザーを割り当てる際、グループを利用して割り当てたいニーズがあると思います。(特定の部署だけに利用を許可するケースなど)
しかしながら、Azure AD でのアプリへのグループの割り当ては Azure AD Premium (P1, P2) の有償ライセンスでのみ可能となっており、Free 版や Office 365 に付いてくる Azure AD ではユーザー単位での割り当てしかできません。
(Free 版の Azure AD で割り当ての追加画面を表示した図)
image.png

大量のユーザーや特定条件に一致するユーザーをアプリに割り当てたい場合

グループに限らず、大量のユーザーや特定条件にあてはまるユーザーをアプリに一括で割り当てたい場合も、GUIでポチポチやるのは手間としても処理速度的にも大変ですね。これは Azure AD の Premium ライセンスがあったとしても同様かと思います。
こういう場合は PowerShell での一括処理をお勧めします。

PowerShell を使用したアプリへのユーザー割り当て

下記はアプリ名とグループ名を指定すると一括でグループ内メンバーをアプリに割り当てるスクリプトです。

$cred = Get-Credential
Connect-AzureAD -Credential $cred
$app_name = "<アプリ名>"
$app_role_name = "User"
$groupId = Get-AzureADGroup -SearchString "<グループ名>"
$ary_names = Get-AzureADGroupMember -ObjectId $groupId.ObjectId | Select-Object Mail
foreach($username in $ary_names){
    $user = Get-AzureADUser -ObjectId $username.Mail
    $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
  }

$ary_names に入れるユーザーを別の条件で指定すれば、その時のニーズに合わせて一括割り当てできると思います。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0