Skip to content

Commit 522ca06

Browse files
Merge pull request #6114 from NikCharlebois/AADServicePrincipal---Fixes
AADServicePrincipal - Fix AppAssignedToRole Assignment on Creation
2 parents 2a8289c + 316a4bd commit 522ca06

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Change log for Microsoft365DSC
22

3-
# 1.25.515.1
3+
# UNRELEASED
4+
5+
* AADServicePrincipal
6+
* Fixed the assignment of AppRoleAssignedTo when creatign a new Service Principal.
7+
8+
# 1.25.514.1
49

510
* AADApplication
611
* Fixed an issue where the `AdminConsentGranted` property had an incorrect value.

Modules/Microsoft365DSC/DSCResources/MSFT_AADServicePrincipal/MSFT_AADServicePrincipal.psm1

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,15 +538,13 @@ function Set-TargetResource
538538
Write-Verbose -Message "Translated to AppId {$($currentParameters.AppId)}"
539539
}
540540

541+
$AppRoleAssignedToSpecified = $currentParameters.ContainsKey('AppRoleAssignedTo')
541542
# ServicePrincipal should exist but it doesn't
542543
if ($Ensure -eq 'Present' -and $currentAADServicePrincipal.Ensure -eq 'Absent')
543544
{
544-
if ($null -ne $AppRoleAssignedTo)
545-
{
546-
$currentParameters.AppRoleAssignedTo = $AppRoleAssignedToValues
547-
}
548545
# removing Delegated permission classifications from this new call, as adding below separately
549546
$currentParameters.Remove('DelegatedPermissionClassifications') | Out-Null
547+
$currentParameters.Remove('AppRoleAssignedTo') | Out-Null
550548

551549
Write-Verbose -Message 'Creating new Service Principal'
552550
Write-Verbose -Message "With Values: $(Convert-M365DscHashtableToString -Hashtable $currentParameters)"
@@ -576,6 +574,40 @@ function Set-TargetResource
576574
Invoke-MgGraphRequest -Uri $Uri -Method Post -Body $params
577575
}
578576
}
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+
}
579611
}
580612
# ServicePrincipal should exist and will be configured to desired state
581613
elseif ($Ensure -eq 'Present' -and $currentAADServicePrincipal.Ensure -eq 'Present')

Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,9 @@ Specifies the name of parameters that should not be assessed as part of the repo
26592659
.Parameter ExcludedResources
26602660
Specifies the name of resources that should not be assessed as part of the report.
26612661
2662+
.Parameter DriftOnly
2663+
Specifies if the report should only show properties drifts and not missing instances.
2664+
26622665
.Example
26632666
Assert-M365DSCBlueprint -BluePrintUrl 'C:\DS\blueprint.m365' -OutputReportPath 'C:\DSC\BlueprintReport.html'
26642667
@@ -2723,7 +2726,11 @@ function Assert-M365DSCBlueprint
27232726

27242727
[Parameter()]
27252728
[System.String[]]
2726-
$ExcludedResources
2729+
$ExcludedResources,
2730+
2731+
[Parameter()]
2732+
[System.Boolean]
2733+
$DriftOnly = $true
27272734
)
27282735

27292736
#Ensure the proper dependencies are installed in the current environment.
@@ -2827,7 +2834,7 @@ function Assert-M365DSCBlueprint
28272834
New-M365DSCDeltaReport -Source $ExportPath `
28282835
-Destination $LocalBluePrintPath `
28292836
-OutputPath $OutputReportPath `
2830-
-DriftOnly:$true `
2837+
-DriftOnly $DriftOnly `
28312838
-IsBlueprintAssessment:$true `
28322839
-HeaderFilePath $HeaderFilePath `
28332840
-Type $Type `

0 commit comments

Comments
 (0)