使用New-MgServicePrincipalAppRoleAssignment代替New-AzureAdServiceAppRoleAssignment的问题

7cwmlq89  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(103)

我尝试在PowerShell中添加图形API角色,并在使用图形而不是AzureAD模块时获得以下通用错误。
错误代码:
“出现一个或多个错误”
InnerException:System.ObjectDisposedException:无法访问已释放的对象。
我正在连接的服务主体在管理员同意下具有以下委托权限:

  • Application.ReadWrite.All
  • AppRoleAssignment.ReadWrite.All
  • DelegatedPermissionGrant.ReadWrite.All

代码:

$ApplicationId = '<Myappid>'
$SecuredPassword = '<MyAppID secret>'
$tenantID = '<MyTenantID>'

$SecuredPasswordPassword = ConvertTo-SecureString -String $SecuredPassword -AsPlainText -Force
$ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecuredPasswordPassword

Connect-MgGraph -TenantId $tenantID -ClientSecretCredential $ClientSecretCredential 

$params = @{
    "PrincipalId" = "<principalID>"  #ObjectID of the enterprise app for my app registration
    "ResourceId" = "resourceID" #ID of graph service principal ID in my tenant
    "AppRoleId" = "approleID" #ID of the graph role
}

  try {
    New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId "<same as resource ID in params; Graph api id>" -BodyParameter $params -ErrorAction:Stop
  }
  catch {
    $tmpError = $_.exception
  }

字符串

2guxujil

2guxujil1#

我创建了一个Azure AD应用程序,并授予了**AppRoleAssignment.ReadWrite.All* 应用程序权限**:


的数据
为Service Principal分配Graph角色,请使用以下PowerShell脚本:

$ApplicationId = "AppID"
$tenantID = "TenantID"

Connect-MgGraph -ClientId $ApplicationId -TenantId $tenantID -CertificateThumbprint "xxxxx"

$params = @{
principalId = "ServicePrincipalObjectID"
resourceId = "MicrosoftGraphObjectID"
appRoleId = "5b567255-7703-4780-807c-7be8301ae99b"
}

New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId ServicePrincipalObjectID -BodyParameter $params

字符串


*Group.Read.All API权限成功分配给服务主体,如下所示:


  • 如果问题仍然存在 *,请检查是否传递了有效的resourceId

ID为00000003-0000-0000-c000-000000000000的应用程序ID


  • 在你的例子中,你传递的ServicePrincipalId作为Graph App ID是无效的,你必须传递与principalId相同的ID。
New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId ServicePrincipalObjectID -BodyParameter $params

  • 在Application上下文中连接MgGraph时,分配Application API权限执行操作。
    参考号:

将appRoleAssignment授予服务主体

相关问题