我正在使用Powershell将角色添加到Azure中的现有应用程序注册中。我正在使用以下命令:
Set-AzureADApplication -ObjectId $myApp.ObjectId -AppRoles $newAppRoles
$newAppROles
是Microsoft.Open.AzureAD.Model.AppRole
当我执行以上命令时,出现此错误:
Set-AzureADApplication:无法将“ System.Collections.Generic.List`1 [Microsoft.Open.AzureAD.Model.AppRole]”转换为参数“ AppRoles”所需的类型“ Microsoft.Open.AzureAD.Model.AppRole” '。不支持指定的方法。
SetAzureADApplication
的文档说,它需要应用角色列表;但我收到此错误。似乎没有其他文档可以帮助我。有人可以告诉我我在做什么错。
下面是完整代码
Connect-AzureAD
$myApp = ""
$appName = "Narasimham POC Powershell - Multiple reply URLs"
if (!($myApp = Get-AzureADApplication -Filter "DisplayName eq '$($appName)'" -ErrorAction SilentlyContinue)) {
Write-Output "Application $appName not found"
}
else {
Write-Output $myApp
$currentAppRoles = $myApp.AppRoles
$appRole = New-Object -TypeName Microsoft.Open.AzureAD.Model.AppRole
$appRole.IsEnabled = $true
$appRole.DisplayName = "Read Role"
$appRole.Value = "Reader"
$appRole.AllowedMemberTypes = "User"
$appRole.Id = New-Guid
$appRole.Description = "Reader Role for Narasimham POC Powershell"
$newAppRoles = @($currentAppRoles, $appRole)
Write-Output $newAppRoles
Set-AzureADApplication -ObjectId $myApp.ObjectId -AppRoles $newAppRoles
}
答案 0 :(得分:0)
我认为问题出在脚本的一部分,该部分为当前角色添加了新角色。
尝试从您的问题中替换脚本的这一部分:
$newAppRoles = @($currentAppRoles, $appRole)
Write-Output $newAppRoles
Set-AzureADApplication -ObjectId $myApp.ObjectId -AppRoles $newAppRoles
使用类似这样的内容:
$currentAppRoles.Add($appRole)
Write-Output $currentAppRoles
Set-AzureADApplication -ObjectId $myApp.ObjectId -AppRoles $currentAppRoles
这里是我之前用来回答very similar SO question的完整脚本,以防对您有帮助。这会将新的应用角色添加到现有的注册应用中:
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