@@ -538,15 +538,13 @@ function Set-TargetResource
538
538
Write-Verbose - Message " Translated to AppId {$ ( $currentParameters.AppId ) }"
539
539
}
540
540
541
+ $AppRoleAssignedToSpecified = $currentParameters.ContainsKey (' AppRoleAssignedTo' )
541
542
# ServicePrincipal should exist but it doesn't
542
543
if ($Ensure -eq ' Present' -and $currentAADServicePrincipal.Ensure -eq ' Absent' )
543
544
{
544
- if ($null -ne $AppRoleAssignedTo )
545
- {
546
- $currentParameters.AppRoleAssignedTo = $AppRoleAssignedToValues
547
- }
548
545
# removing Delegated permission classifications from this new call, as adding below separately
549
546
$currentParameters.Remove (' DelegatedPermissionClassifications' ) | Out-Null
547
+ $currentParameters.Remove (' AppRoleAssignedTo' ) | Out-Null
550
548
551
549
Write-Verbose - Message ' Creating new Service Principal'
552
550
Write-Verbose - Message " With Values: $ ( Convert-M365DscHashtableToString - Hashtable $currentParameters ) "
@@ -576,6 +574,40 @@ function Set-TargetResource
576
574
Invoke-MgGraphRequest - Uri $Uri - Method Post - Body $params
577
575
}
578
576
}
577
+
578
+ # Update AppRoleAssignedTo
579
+ if ($AppRoleAssignedToSpecified )
580
+ {
581
+ Write-Verbose - Message " Updating AppRoleAssignedTo value"
582
+ foreach ($assignment in $AppRoleAssignedTo )
583
+ {
584
+ $AppRoleAssignedToValues += @ {
585
+ PrincipalType = $assignment.PrincipalType
586
+ Identity = $assignment.Identity
587
+ }
588
+
589
+ if ($assignment.PrincipalType -eq ' User' )
590
+ {
591
+ Write-Verbose - Message " Retrieving user {$ ( $assignment.Identity ) }"
592
+ $user = Get-MgUser - Filter " startswith(UserPrincipalName, '$ ( $assignment.Identity ) ')"
593
+ $PrincipalIdValue = $user.Id
594
+ }
595
+ else
596
+ {
597
+ Write-Verbose - Message " Retrieving group {$ ( $assignment.Identity ) }"
598
+ $group = Get-MgGroup - Filter " DisplayName eq '$ ( $assignment.Identity ) '"
599
+ $PrincipalIdValue = $group.Id
600
+ }
601
+ $bodyParam = @ {
602
+ principalId = $PrincipalIdValue
603
+ resourceId = $newSP.Id
604
+ appRoleId = ' 00000000-0000-0000-0000-000000000000'
605
+ }
606
+ Write-Verbose - Message " Adding Service Principal AppRoleAssignedTo with values:`r`n $ ( ConvertTo-Json $bodyParam - Depth 3 ) "
607
+ New-MgServicePrincipalAppRoleAssignedTo - ServicePrincipalId $newSP.Id `
608
+ - BodyParameter $bodyParam | Out-Null
609
+ }
610
+ }
579
611
}
580
612
# ServicePrincipal should exist and will be configured to desired state
581
613
elseif ($Ensure -eq ' Present' -and $currentAADServicePrincipal.Ensure -eq ' Present' )
0 commit comments