gpt4 book ai didi

powershell - 如何使用 Powershell 脚本在 Azure Active Directory 的 list 下添加应用程序角色

转载 作者:行者123 更新时间:2023-12-03 23:44:36 25 4
gpt4 key购买 nike

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

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

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

最佳答案

您可以在使用 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

关于powershell - 如何使用 Powershell 脚本在 Azure Active Directory 的 list 下添加应用程序角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51651889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com