-
Notifications
You must be signed in to change notification settings - Fork 2
/
AddRolesToGroup.ps1
68 lines (62 loc) · 2.83 KB
/
AddRolesToGroup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Requires -Version 7
#Requires -Modules Az.Accounts
function Add-AADRoleToPrivilegedAccessGroup {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory, ParameterSetName = "RoleName")]
[string]$RoleName,
[Parameter(Mandatory, ParameterSetName = "RoleID")]
[string]$RoleID,
[Parameter(Mandatory)]
[string]$GroupObjectID
)
begin {
# Check if Connect-AzAccount is connected
if ([string]::IsNullOrEmpty($((Get-AzContext).Account))) {
Write-Output "Connect to Azure using Connect-AzAccount first"
break
} else {
$pimtoken = (Get-AzAccessToken -ResourceUrl 'https://api.azrbac.mspim.azure.com' -ErrorAction Stop).Token
$Headers = @{
"Authorization" = "Bearer {0}" -f ($pimtoken)
}
}
# Get all roles from Azure AD
$azureADRoles = ((Invoke-AzRestMethod -Uri 'https://graph.microsoft.com/v1.0/roleManagement/directory/roleDefinitions').Content | ConvertFrom-Json).Value
if ([string]::IsNullOrEmpty($($azureADRoles))) {
Throw "Could not get Azure AD roles from Azure AD."
}
if ($RoleName) {
$RoleID = ($azureADRoles | Where-Object { $_.displayName -eq $RoleName }).id
}
# Check if the role exists in Azure AD
$RoleNameFromAAD = ($azureADRoles | Where-Object { $_.id -eq $RoleID }).displayName
if ([string]::IsNullOrEmpty($($RoleNameFromAAD))) {
Throw "The role '$RoleID$RoleName' does not exist in Azure AD. Cannot continue."
}
# URI to the PIM API endpoint for adding role assignments
$AddRolesToGroupURI = "https://api.azrbac.mspim.azure.com/api/v2/privilegedAccess/aadroles/roleAssignmentRequests"
}
process {
$roleObject = [PSCustomObject]@{
resourceId = (Get-AzContext).Tenant.Id # Tenant ID
roleDefinitionId = $RoleID # Role definition ID to add from Azure AD
subjectId = $GroupObjectID # Privileged Access Group ID
assignmentState = 'Active' # Create active role assignment for the privileged access group
type = 'AdminAdd'
reason = 'Deployment from script'
schedule = @{
type = 'Once'
startDateTime = Get-Date
endDateTime = $null
}
scopedResourceId = ""
condition = $null
conditionVersion = $null
} | ConvertTo-Json
Write-Output "Adding role $RoleNameFromAAD with role ID $RoleID to Azure AD Group with ID $GroupObjectID"
Invoke-RestMethod -Uri $AddRolesToGroupURI -Headers $Headers -Method POST -Body $roleObject -ContentType 'application/json'
}
end {
}
}