【问题标题】:How to add app roles under manifest in Azure Active Directory using Powershell script如何使用 Powershell 脚本在 Azure Active Directory 的清单下添加应用角色
【发布时间】:2019-01-10 02:57:22
【问题描述】:

我手动创建了 Azure Active Directory 应用程序。我想通过 PowerShell 脚本添加用户并分配用户角色。

我可以使用 PowerShell 脚本添加用户,但无法在 azure Active Directory 应用程序的清单下添加应用角色。

是否可以通过 PowerShell 脚本添加应用角色?

【问题讨论】:

标签: powershell azure azure-active-directory


【解决方案1】:

您可以在使用 New-AzureADApplication 创建新应用程序或使用 Set-AzureADApplication 为现有应用程序创建新应用程序时执行此操作。我没有看到专门用于添加/删除角色的命令,这就是上述两个选项的原因。

这是一个用于将新应用角色添加到现有注册应用程序的示例 PowerShell 脚本:

Connect-AzureAD -TenantId <Tenant GUID>

# Create an application role of given name and description
Function CreateAppRole([string] $Name, [string] $Description)
{
    $appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
    $appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
    $appRole.AllowedMemberTypes.Add("User");
    $appRole.DisplayName = $Name
    $appRole.Id = New-Guid
    $appRole.IsEnabled = $true
    $appRole.Description = $Description
    $appRole.Value = $Name;
    return $appRole
}

# ObjectId for application from App Registrations in your AzureAD
$appObjectId = "<Your Application Object Id>"
$app = Get-AzureADApplication -ObjectId $appObjectId
$appRoles = $app.AppRoles
Write-Host "App Roles before addition of new role.."
Write-Host $appRoles

$newRole = CreateAppRole -Name "MyNewApplicationRole" -Description "This is my new Application Role"
$appRoles.Add($newRole)

Set-AzureADApplication -ObjectId $app.ObjectId -AppRoles $appRoles

完成上述脚本以添加 AppRole 后,为用户分配角色非常简单,并且可以使用直接命令。这是一个示例脚本 -

# Assign the values to the variables
$username = "<You user's UPN>"
$app_name = "<Your App's display name>"
$app_role_name = "<App role display name>"

# Get the user to assign, and the service principal for the app to assign to
$user = Get-AzureADUser -ObjectId "$username"
$sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
$appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }

# Assign the user to the app role
New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id

【讨论】:

  • 您好 Neo99,我想跳过登录 AzureAD 的弹出窗口。所以我做了参数化。但出现错误“必须运行 Connect-AzureAD”
  • $ssAADKey = ConvertTo-SecureString $AADKey -AsPlainText -Force $psCredential = New-Object System.Management.Automation.PSCredential($AADAppID, $ssAADKey) #Connect-AzureRmAccount -ServicePrincipal -Credential $psCredential -TenantId $tenantId Connect-AzureAD -Credential $psCredential Set-AzureRMContext -Subscription $subId -NAME $SubscriptionName
  • 嗨@rohit,你能不能看看这个:stackoverflow.com/questions/53233876/…
猜你喜欢
  • 2016-10-07
  • 2019-01-16
  • 1970-01-01
  • 2019-06-11
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 2014-06-16
  • 1970-01-01
相关资源
最近更新 更多