Repository navigation
Expand file tree
/
Copy pathAssign-CorrelatedUsers.ps1
More file actions
654 lines (543 loc) ยท 26.6 KB
/
Copy pathAssign-CorrelatedUsers.ps1
File metadata and controls
654 lines (543 loc) ยท 26.6 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<#
.SYNOPSIS
Assigns correlated-but-unassigned users from an identity correlation report
to an enterprise application, with CSV-based routing to access packages.
.DESCRIPTION
1. Lists correlation reports for a service principal and picks the latest.
2. Retrieves identities with status 'correlatedNotAssigned' and 'correlatedAssigned'.
3. Assigns correlatedNotAssigned users to the enterprise application via appRoleAssignedTo.
4. (Optional) Routes ALL correlated users (both assigned and not-yet-assigned)
to access packages based on rules in a CSV file.
Rules match against targetIdentity.details.Properties (the SCIM property bag).
ALL matching rule groups result in assignment (a user can get multiple packages).
Rules CSV columns:
RuleGroup - rows sharing the same group number are AND-ed together.
Different groups are evaluated independently.
PropertyName - key in the target property bag (e.g. title, userType,
displayName, userName, name.givenName).
Use "source.userPrincipalName" to match on
sourceIdentity.matchingProperty.value.
Operator - eq | ne | contains | startswith | endswith | regex
Value - the value to compare against (case-insensitive)
AccessPackageId - the access package to assign when the group matches
PolicyId - the assignment policy for that access package
.PARAMETER ServicePrincipalId
The object ID of the service principal (enterprise application).
.PARAMETER AppRoleId
The app role ID to assign. Defaults to the "Default Access" role (all-zeros GUID).
.PARAMETER RulesFile
Path to a CSV file with routing rules. See description for column format.
.PARAMETER AccessPackageId
(Optional) Fallback access package ID when no rules match a user
(requires -FallbackBehavior UseFallback). Also used when -RulesFile is
not provided (simple mode, same as original script).
.PARAMETER PolicyId
(Optional) Fallback/simple-mode assignment policy ID.
.PARAMETER FallbackBehavior
What to do when no rule matches a user (only relevant with -RulesFile).
- Skip : do not assign an access package (default).
- UseFallback : assign to -AccessPackageId / -PolicyId.
.PARAMETER SkipAppRoleAssignment
If specified, skips step 4 (enterprise app role assignment). Useful when
users are already assigned to the app and you only want to manage access packages.
.PARAMETER OutputFile
(Optional) Path to a CSV file where results will be exported for audit trail.
.PARAMETER DryRun
Lists what would be done without making changes.
.EXAMPLE
# Simple mode (no rules) - same behavior as original script
.\Assign-CorrelatedUsers.ps1 -ServicePrincipalId "7A22..." `
-AccessPackageId "a914..." -PolicyId "2264..."
.EXAMPLE
# CSV rules with fallback
.\Assign-CorrelatedUsers.ps1 -ServicePrincipalId "7A22..." `
-RulesFile ".\access-package-rules.csv" `
-AccessPackageId "fallback-pkg" -PolicyId "fallback-pol" `
-FallbackBehavior UseFallback -DryRun
.EXAMPLE
# CSV rules, skip unmatched users
.\Assign-CorrelatedUsers.ps1 -ServicePrincipalId "7A22..." `
-RulesFile ".\access-package-rules.csv" -DryRun
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ServicePrincipalId,
[Parameter()]
[string]$AppRoleId = "00000000-0000-0000-0000-000000000000",
[Parameter()]
[string]$RulesFile,
[Parameter()]
[string]$AccessPackageId,
[Parameter()]
[string]$PolicyId,
[Parameter()]
[ValidateSet("Skip", "UseFallback")]
[string]$FallbackBehavior = "Skip",
[Parameter()]
[switch]$SkipAppRoleAssignment,
[Parameter()]
[string]$OutputFile,
[Parameter()]
[switch]$DryRun
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# ---------------------------------------------------------------------------
# Results log
# ---------------------------------------------------------------------------
$resultsLog = [System.Collections.Generic.List[PSCustomObject]]::new()
function Add-ResultEntry {
param(
[string]$UserId,
[string]$DisplayName,
[string]$Status,
[string]$Step,
[string]$Action,
[string]$AccessPackageId,
[string]$PolicyId,
[string]$Details
)
$resultsLog.Add([PSCustomObject]@{
Timestamp = (Get-Date -Format 'o')
UserId = $UserId
DisplayName = $DisplayName
CorrelationStatus = $Status
Step = $Step
Action = $Action
AccessPackageId = $AccessPackageId
PolicyId = $PolicyId
Details = $Details
})
}
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
function Invoke-GraphRequest {
param(
[Parameter(Mandatory)][string]$Method,
[Parameter(Mandatory)][string]$Uri,
[Parameter()][object]$Body
)
$params = @{
Method = $Method
Uri = $Uri
}
if ($Body) {
$params.Body = ($Body | ConvertTo-Json -Depth 10)
$params.ContentType = "application/json"
}
Invoke-MgGraphRequest @params
}
function Get-AllPages {
param([Parameter(Mandatory)][string]$Uri)
$results = @()
$currentUri = $Uri
while ($currentUri) {
$response = Invoke-GraphRequest -Method GET -Uri $currentUri
if ($response.ContainsKey('value') -and $response.value) {
$results += $response.value
}
$currentUri = if ($response.ContainsKey('@odata.nextLink')) { $response.'@odata.nextLink' } else { $null }
}
return $results
}
function Get-IdentityUserId {
param([Parameter(Mandatory)]$Identity)
$sourceIdentity = $Identity['sourceIdentity']
$anchor = if ($sourceIdentity) { $sourceIdentity['anchor'] } else { $null }
$uid = if ($anchor) { $anchor['value'] } else { $null }
if (-not $uid -and $sourceIdentity) { $uid = $sourceIdentity['id'] }
if (-not $uid) { $uid = $Identity['id'] }
return $uid
}
function Get-IdentityDisplayName {
param([Parameter(Mandatory)]$Identity)
# Try to pull displayName from the target property bag
$props = Get-TargetProperties -Identity $Identity
if ($props -and $props.ContainsKey('displayName')) {
return ($props['displayName'] | Select-Object -First 1)
}
$sourceIdentity = $Identity['sourceIdentity']
$dn = if ($sourceIdentity) { $sourceIdentity['displayName'] } else { $null }
if (-not $dn) { $dn = $Identity['displayName'] }
if (-not $dn) { $dn = Get-IdentityUserId -Identity $Identity }
return $dn
}
function Get-TargetProperties {
param([Parameter(Mandatory)]$Identity)
$targetIdentity = $Identity['targetIdentity']
$details = if ($targetIdentity) { $targetIdentity['details'] } else { $null }
$raw = if ($details) { $details['Properties'] } else { $null }
if (-not $raw) { return @{} }
try { return ($raw | ConvertFrom-Json -AsHashtable) }
catch { return @{} }
}
function Test-RuleCondition {
param(
[string]$PropertyValue,
[string]$Operator,
[string]$RuleValue
)
# Both sides are compared case-insensitively
switch ($Operator.ToLower()) {
'eq' { return $PropertyValue -ieq $RuleValue }
'ne' { return $PropertyValue -ine $RuleValue }
'contains' { return $PropertyValue -imatch [regex]::Escape($RuleValue) }
'startswith' { return $PropertyValue.StartsWith($RuleValue, [System.StringComparison]::OrdinalIgnoreCase) }
'endswith' { return $PropertyValue.EndsWith($RuleValue, [System.StringComparison]::OrdinalIgnoreCase) }
'regex' { return $PropertyValue -imatch $RuleValue }
default { Write-Warning "Unknown operator '$Operator'. Treating as false."; return $false }
}
}
function Get-MatchingPackages {
<#
.SYNOPSIS
Evaluates all rule groups against one identity. Returns an array of
@{ AccessPackageId; PolicyId } for every group whose conditions ALL match.
#>
param(
[Parameter(Mandatory)]$Identity,
[Parameter(Mandatory)][array]$RuleGroups # grouped rules keyed by RuleGroup
)
$targetProps = Get-TargetProperties -Identity $Identity
$sourceUpn = $Identity.sourceIdentity.matchingProperty.value
$matched = @()
foreach ($group in $RuleGroups) {
$allConditionsMet = $true
foreach ($rule in $group.Rules) {
$propName = $rule.PropertyName.Trim()
$propValue = $null
if ($propName -ieq 'source.userPrincipalName') {
$propValue = $sourceUpn
}
elseif ($targetProps.ContainsKey($propName)) {
# Properties are arrays; take first value
$propValue = ($targetProps[$propName] | Select-Object -First 1)
}
if ($null -eq $propValue) {
$allConditionsMet = $false
break
}
if (-not (Test-RuleCondition -PropertyValue $propValue -Operator $rule.Operator -RuleValue $rule.Value)) {
$allConditionsMet = $false
break
}
}
if ($allConditionsMet) {
$matched += @{
AccessPackageId = $group.AccessPackageId
PolicyId = $group.PolicyId
}
}
}
return $matched
}
# ---------------------------------------------------------------------------
# Validate parameters
# ---------------------------------------------------------------------------
if ($RulesFile -and -not (Test-Path $RulesFile)) {
Write-Error "Rules file not found: $RulesFile"
return
}
if (-not $RulesFile -and $AccessPackageId -and -not $PolicyId) {
Write-Error "AccessPackageId was provided but PolicyId is missing. Both are required."
return
}
if ($FallbackBehavior -eq 'UseFallback' -and (-not $AccessPackageId -or -not $PolicyId)) {
Write-Error "FallbackBehavior is 'UseFallback' but AccessPackageId or PolicyId is missing."
return
}
# ---------------------------------------------------------------------------
# Load rules if provided
# ---------------------------------------------------------------------------
$ruleGroups = @()
if ($RulesFile) {
Write-Host "Loading rules from: $RulesFile" -ForegroundColor Cyan
$csvRows = Import-Csv -Path $RulesFile
# Validate CSV columns
$requiredCols = @('RuleGroup', 'PropertyName', 'Operator', 'Value', 'AccessPackageId', 'PolicyId')
$actualCols = $csvRows[0].PSObject.Properties.Name
foreach ($col in $requiredCols) {
if ($col -notin $actualCols) {
Write-Error "Rules CSV is missing required column '$col'. Expected columns: $($requiredCols -join ', ')"
return
}
}
# Group rows by RuleGroup โ rows in the same group are AND-ed
$grouped = $csvRows | Group-Object -Property RuleGroup
foreach ($g in $grouped) {
$firstRow = $g.Group[0]
$ruleGroups += @{
GroupId = $g.Name
AccessPackageId = $firstRow.AccessPackageId.Trim()
PolicyId = $firstRow.PolicyId.Trim()
Rules = $g.Group | ForEach-Object {
@{
PropertyName = $_.PropertyName
Operator = $_.Operator
Value = $_.Value
}
}
}
}
Write-Host " Loaded $($ruleGroups.Count) rule group(s) from $($csvRows.Count) row(s)." -ForegroundColor Green
foreach ($rg in $ruleGroups) {
$condDesc = ($rg.Rules | ForEach-Object { "$($_.PropertyName) $($_.Operator) '$($_.Value)'" }) -join ' AND '
Write-Host " Group $($rg.GroupId): $condDesc -> Package $($rg.AccessPackageId)" -ForegroundColor DarkCyan
}
}
$needsEntitlementMgmt = $RulesFile -or $AccessPackageId
# ---------------------------------------------------------------------------
# Ensure Microsoft Graph connection
# ---------------------------------------------------------------------------
$requiredScopes = @(
"AuditLog.Read.All",
"AppRoleAssignment.ReadWrite.All",
"Application.Read.All"
)
if ($needsEntitlementMgmt) {
$requiredScopes += "EntitlementManagement.ReadWrite.All"
}
$context = Get-MgContext
if (-not $context) {
Write-Host "Connecting to Microsoft Graph..." -ForegroundColor Cyan
Connect-MgGraph -Scopes $requiredScopes -NoWelcome
}
else {
Write-Host "Already connected to Microsoft Graph as $($context.Account)" -ForegroundColor Green
}
# ---------------------------------------------------------------------------
# Step 1 - Get the latest correlation report for the service principal
# ---------------------------------------------------------------------------
Write-Host "`n=== Step 1: Fetching correlation reports ===" -ForegroundColor Cyan
$correlationsUri = "https://graph.microsoft.com/beta/reports/correlations?`$filter=servicePrincipal/id eq '$ServicePrincipalId'&`$orderby=startDateTime desc&`$top=1"
$correlationsResponse = Invoke-GraphRequest -Method GET -Uri $correlationsUri
if (-not $correlationsResponse.value -or $correlationsResponse.value.Count -eq 0) {
Write-Error "No correlation reports found for service principal '$ServicePrincipalId'."
return
}
$latestReport = $correlationsResponse.value[0]
$correlationId = $latestReport.id
Write-Host "Latest correlation report:" -ForegroundColor Green
Write-Host " ID: $correlationId"
Write-Host " Start: $($latestReport.startDateTime)"
Write-Host " End: $($latestReport.endDateTime)"
# ---------------------------------------------------------------------------
# Step 2 - Show summary counts
# ---------------------------------------------------------------------------
Write-Host "`n=== Step 2: Identity summary ===" -ForegroundColor Cyan
$baseIdentitiesUri = "https://graph.microsoft.com/beta/reports/correlations/$correlationId/identities"
foreach ($status in @('uncorrelated', 'correlatedNotAssigned', 'correlatedAssigned')) {
$countUri = "${baseIdentitiesUri}/`$count?`$filter=status eq '$status'"
try {
$count = Invoke-MgGraphRequest -Method GET -Uri $countUri -Headers @{ "ConsistencyLevel" = "eventual" }
Write-Host " ${status}: $count"
}
catch {
Write-Host " ${status}: (count unavailable)" -ForegroundColor Yellow
}
}
# ---------------------------------------------------------------------------
# Step 3a - Retrieve correlatedNotAssigned identities (for enterprise app)
# ---------------------------------------------------------------------------
Write-Host "`n=== Step 3a: Retrieving correlatedNotAssigned identities ===" -ForegroundColor Cyan
$filterUri = "${baseIdentitiesUri}?`$filter=status eq 'correlatedNotAssigned'"
$rawUnassigned = @(Get-AllPages -Uri $filterUri)
# Verify actual status โ the API $filter may return extra identities
$unassignedIdentities = @($rawUnassigned | Where-Object { $_.status -eq 'correlatedNotAssigned' })
if ($rawUnassigned.Count -ne $unassignedIdentities.Count) {
Write-Host " Note: API returned $($rawUnassigned.Count) identities but only $($unassignedIdentities.Count) have status 'correlatedNotAssigned' (filtered client-side)." -ForegroundColor DarkYellow
}
if ($unassignedIdentities.Count -eq 0) {
Write-Host "No correlatedNotAssigned identities found." -ForegroundColor Yellow
}
else {
Write-Host "Found $($unassignedIdentities.Count) correlatedNotAssigned identities." -ForegroundColor Green
}
# ---------------------------------------------------------------------------
# Step 3b - Retrieve correlatedAssigned identities (for access packages)
# ---------------------------------------------------------------------------
Write-Host "`n=== Step 3b: Retrieving correlatedAssigned identities ===" -ForegroundColor Cyan
$assignedFilterUri = "${baseIdentitiesUri}?`$filter=status eq 'correlatedAssigned'"
$rawAssigned = @(Get-AllPages -Uri $assignedFilterUri)
# Verify actual status client-side
$assignedIdentities = @($rawAssigned | Where-Object { $_.status -eq 'correlatedAssigned' })
if ($rawAssigned.Count -ne $assignedIdentities.Count) {
Write-Host " Note: API returned $($rawAssigned.Count) identities but only $($assignedIdentities.Count) have status 'correlatedAssigned' (filtered client-side)." -ForegroundColor DarkYellow
}
if ($assignedIdentities.Count -eq 0) {
Write-Host "No correlatedAssigned identities found." -ForegroundColor Yellow
}
else {
Write-Host "Found $($assignedIdentities.Count) correlatedAssigned identities." -ForegroundColor Green
}
$allCorrelatedIdentities = @($unassignedIdentities) + @($assignedIdentities)
if ($allCorrelatedIdentities.Count -eq 0) {
Write-Host "No correlated identities found at all. Nothing to do." -ForegroundColor Yellow
return
}
Write-Host "Total correlated identities: $($allCorrelatedIdentities.Count) ($($unassignedIdentities.Count) unassigned + $($assignedIdentities.Count) assigned)" -ForegroundColor Green
# ---------------------------------------------------------------------------
# Step 4 - Assign users to the enterprise application
# ---------------------------------------------------------------------------
Write-Host "`n=== Step 4: Assigning correlatedNotAssigned users to enterprise app ===" -ForegroundColor Cyan
if ($SkipAppRoleAssignment) {
Write-Host "Skipping enterprise app assignment (-SkipAppRoleAssignment specified)." -ForegroundColor DarkGray
$assignedCount = 0
$failedCount = 0
}
elseif ($unassignedIdentities.Count -eq 0) {
Write-Host "No correlatedNotAssigned users to assign. Skipping." -ForegroundColor DarkGray
$assignedCount = 0
$failedCount = 0
}
else {
$appRoleUri = "https://graph.microsoft.com/v1.0/servicePrincipals/$ServicePrincipalId/appRoleAssignedTo"
$assignedCount = 0
$failedCount = 0
foreach ($identity in $unassignedIdentities) {
$userId = Get-IdentityUserId -Identity $identity
$displayName = Get-IdentityDisplayName -Identity $identity
if (-not $userId) {
Write-Warning "Skipping identity with no resolvable user ID: $($identity | ConvertTo-Json -Depth 3 -Compress)"
Add-ResultEntry -UserId '' -DisplayName '' -Status $identity.status -Step 'AppRole' -Action 'Failed' -Details 'No resolvable user ID'
$failedCount++
continue
}
if ($DryRun) {
Write-Host " [DRY RUN] Would assign user '$displayName' ($userId) to app role." -ForegroundColor Yellow
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AppRole' -Action 'DryRun' -Details "AppRoleId=$AppRoleId"
$assignedCount++
continue
}
$body = @{
principalId = $userId
resourceId = $ServicePrincipalId
appRoleId = $AppRoleId
}
try {
Invoke-GraphRequest -Method POST -Uri $appRoleUri -Body $body | Out-Null
Write-Host " Assigned '$displayName' ($userId) to enterprise app." -ForegroundColor Green
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AppRole' -Action 'Assigned' -Details "AppRoleId=$AppRoleId"
$assignedCount++
}
catch {
$errorMsg = $_.Exception.Message
if ($errorMsg -match "Permission being assigned already exists") {
Write-Host " Skipped '$displayName' ($userId) - already assigned." -ForegroundColor DarkGray
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AppRole' -Action 'AlreadyAssigned'
$assignedCount++
}
else {
Write-Warning " Failed to assign '$displayName' ($userId): $errorMsg"
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AppRole' -Action 'Failed' -Details $errorMsg
$failedCount++
}
}
}
} # end else (not skipped)
Write-Host "`nEnterprise app assignment complete: $assignedCount succeeded, $failedCount failed." -ForegroundColor Cyan
# ---------------------------------------------------------------------------
# Step 5 - Assign users to access packages
# ---------------------------------------------------------------------------
if (-not $RulesFile -and -not $AccessPackageId) {
Write-Host "`nNo access package assignment requested. Skipping step 5." -ForegroundColor DarkGray
Write-Host "`nAll done." -ForegroundColor Green
return
}
Write-Host "`n=== Step 5: Assigning all correlated users to access packages ===" -ForegroundColor Cyan
Write-Host "Processing $($allCorrelatedIdentities.Count) correlated identities for access package routing..." -ForegroundColor Cyan
$assignmentRequestUri = "https://graph.microsoft.com/v1.0/identityGovernance/entitlementManagement/assignmentRequests"
$pkgAssignedCount = 0
$pkgSkippedCount = 0
$pkgFailedCount = 0
foreach ($identity in $allCorrelatedIdentities) {
$userId = Get-IdentityUserId -Identity $identity
$displayName = Get-IdentityDisplayName -Identity $identity
if (-not $userId) {
Add-ResultEntry -UserId '' -DisplayName '' -Status $identity.status -Step 'AccessPackage' -Action 'Failed' -Details 'No resolvable user ID'
$pkgFailedCount++
continue
}
# Determine which packages this user should be assigned to
$packages = @()
if ($RulesFile) {
$packages = @(Get-MatchingPackages -Identity $identity -RuleGroups $ruleGroups)
if ($packages.Count -eq 0) {
if ($FallbackBehavior -eq 'UseFallback' -and $AccessPackageId -and $PolicyId) {
$packages = @(@{ AccessPackageId = $AccessPackageId; PolicyId = $PolicyId })
Write-Host " No rule matched '$displayName' - using fallback package." -ForegroundColor DarkYellow
}
else {
Write-Host " No rule matched '$displayName' ($userId) - skipping." -ForegroundColor DarkGray
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AccessPackage' -Action 'NoRuleMatch'
$pkgSkippedCount++
continue
}
}
else {
$pkgIds = ($packages | ForEach-Object { $_.AccessPackageId }) -join ', '
Write-Host " '$displayName' matched $($packages.Count) rule(s) -> packages: $pkgIds" -ForegroundColor DarkCyan
}
}
else {
# Simple mode - single package for everyone
$packages = @(@{ AccessPackageId = $AccessPackageId; PolicyId = $PolicyId })
}
foreach ($pkg in $packages) {
# Pre-check: skip if user already has an active assignment for this package
$existingUri = "https://graph.microsoft.com/v1.0/identityGovernance/entitlementManagement/assignments?`$filter=target/objectId eq '$userId' and accessPackage/id eq '$($pkg.AccessPackageId)'&`$select=id,state"
try {
$existing = Invoke-GraphRequest -Method GET -Uri $existingUri
$activeAssignment = $existing.value | Where-Object { $_.state -eq 'delivered' -or $_.state -eq 'delivering' }
if ($activeAssignment) {
Write-Host " Skipped '$displayName' ($userId) - already assigned to package $($pkg.AccessPackageId)." -ForegroundColor DarkGray
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AccessPackage' -Action 'AlreadyAssigned' -AccessPackageId $pkg.AccessPackageId -PolicyId $pkg.PolicyId
$pkgSkippedCount++
continue
}
}
catch {
# If the check fails, proceed with the assignment attempt
Write-Host " Could not verify existing assignment for '$displayName' - proceeding." -ForegroundColor DarkYellow
}
if ($DryRun) {
Write-Host " [DRY RUN] Would assign '$displayName' ($userId) to package $($pkg.AccessPackageId) with policy $($pkg.PolicyId)." -ForegroundColor Yellow
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AccessPackage' -Action 'DryRun' -AccessPackageId $pkg.AccessPackageId -PolicyId $pkg.PolicyId
$pkgAssignedCount++
continue
}
$body = @{
requestType = "adminAdd"
assignment = @{
targetId = $userId
assignmentPolicyId = $pkg.PolicyId
accessPackageId = $pkg.AccessPackageId
}
}
try {
Invoke-GraphRequest -Method POST -Uri $assignmentRequestUri -Body $body | Out-Null
Write-Host " Assigned '$displayName' ($userId) to package $($pkg.AccessPackageId)." -ForegroundColor Green
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AccessPackage' -Action 'Assigned' -AccessPackageId $pkg.AccessPackageId -PolicyId $pkg.PolicyId
$pkgAssignedCount++
}
catch {
Write-Warning " Failed package request for '$displayName' ($userId) pkg=$($pkg.AccessPackageId): $($_.Exception.Message)"
Add-ResultEntry -UserId $userId -DisplayName $displayName -Status $identity.status -Step 'AccessPackage' -Action 'Failed' -AccessPackageId $pkg.AccessPackageId -PolicyId $pkg.PolicyId -Details $_.Exception.Message
$pkgFailedCount++
}
}
}
Write-Host "`nAccess package results: $pkgAssignedCount assigned, $pkgSkippedCount skipped (no match or already assigned), $pkgFailedCount failed." -ForegroundColor Cyan
# ---------------------------------------------------------------------------
# Export results
# ---------------------------------------------------------------------------
if ($OutputFile) {
$resultsLog | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
Write-Host "`nResults exported to: $OutputFile ($($resultsLog.Count) entries)" -ForegroundColor Cyan
}
# ---------------------------------------------------------------------------
# Done
# ---------------------------------------------------------------------------
Write-Host "`nAll done." -ForegroundColor Green