此问题是如何使用 Microsoft Graph Powershell 模块从 Microsoft Entry Id 中的特权身份管理读取我的角色?
在上述问题中我学会了如何阅读我的角色:
$ScopeTypes=@('subscription','resourcegroup')
Connect-AzAccount > $null
Get-AzRoleEligibilitySchedule -Scope "/" -Filter "asTarget()" `
| Where-Object { $ScopeTypes -contains $_.ScopeType } `
| Group-Object RoleDefinitionDisplayName, Scope `
| Select-Object @{ Expression = { $_.group[0] } ; Label = 'Item' } `
| Select-Object -ExpandProperty item `
| Format-Table RoleDefinitionDisplayName, ScopeDisplayName, ScopeType
效果很好:
RoleDefinitionDisplayName ScopeDisplayName ScopeType
------------------------- ---------------- ---------
Azure Kubernetes Service Cluster User Role ... resourcegroup
Cognitive Services Contributor ... subscription
Contributor ... resourcegroup
Contributor ... resourcegroup
Owner ... resourcegroup
Reader ... subscription
Reader ... subscription
现在我想激活它们。
我尝试了以下代码:
$ScopeTypes = @('resourcegroup', 'subscription')
Connect-AzAccount > $null
Get-AzRoleEligibilitySchedule -Scope "/" -Filter "asTarget()" `
| Where-Object { $ScopeTypes -contains $_.ScopeType } `
| Group-Object RoleDefinitionDisplayName, Scope `
| Select-Object @{ Expression = { $_.group[0] } ; Label = 'Item' } `
| Select-Object -ExpandProperty item `
| ForEach-Object {
$p = @{
Name = (New-Guid).Guid
Scope = $_.Scope
PrincipalId = $_.PrincipalId
RoleDefinitionId = $_.RoleDefinitionId
ScheduleInfoStartDateTime = Get-Date -Format o
}
New-AzRoleAssignmentScheduleRequest @p -ExpirationDuration PT8H -ExpirationType AfterDuration -RequestType SelfActivate -Justification "work"
}
不幸的是,所有尝试都失败并出现相同的错误:
New-AzRoleAssignmentScheduleRequest_CreateExpanded: C:\dayforce\scripts\pim.ps1:28
Line |
28 | New-AzRoleAssignmentScheduleRequest @p -ExpirationDuration PT8H - …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The requestor a...e does not have permissions for this request. Please use
| $filter=asTarget() to filter on the requestor's assignments.
其中
a...e
确实是我的对象 ID。我作为该用户在浏览器中激活这些角色没有问题,但它在控制台上不起作用。
我发现出了什么问题。在我的例子中,由
PrincipalId
报告的 Get-AzRoleEligibilitySchedule
是 AAD group 的 ID。但是,激活角色时必须传递用户自己的对象 ID,而不是用户可能所属的 AAD 组的对象 ID。
一旦我更改了代码以传递我自己的对象 Id 而不是
PrincipalId
报告的 Get-AzRoleEligibilitySchedule
- 一切就开始起作用了。