Get-M365DSCCompiledPermissionList Required Roles issue
Description of the issue
When running Get-M365DSCCompiledPermissionList -ResourceNameList (Get-M365DSCAllResources) -PermissionType 'Application' -AccessType 'Update' (https://microsoft365dsc.com/user-guide/cmdlets/Get-M365DSCCompiledPermissionList/) the required roles are always empty and will return an error.
Select-Object : Property "Update" cannot be found.
At D:\a\1\s\Modules\Microsoft365DSC\1.26.128.1\Modules\M365DSCPermissions.psm1:420 char:54
+ ... = $results.RequiredRoles | Select-Object -ExpandProperty $AccessType
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (System.Collections.Hashtable:PSObject) [Select-Object], PSArgumentExce
ption
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
In reality I'm seeing this even for single resources. eg:
Get-M365DSCCompiledPermissionList -ResourceNameList AADUserFlowAttribute -PermissionType 'Application' -AccessType 'Update'
Last working version was 1.26.114.1 (the follow up1.26.121.1 had the hash table issues).
Problematic line: https://github.com/microsoft/Microsoft365DSC/blob/7b77a042bd1c8b3c6e6eb3cf11332fa265e704bd/Modules/Microsoft365DSC/Modules/M365DSCPermissions.psm1#L420
Microsoft 365 DSC Version
1.26.128.1
Environment Information + PowerShell Version
PSVersion 5.1
(on Desktop & Azure Devops)
If I might add a second issue/change on top (I assume they are related though didn't research yet) - the Update-M365DSCAzureAdApplication also seems to be affected by the recent change to the Get-M365DSCCompiledPermissionList from above. It expects a .permissions object.
https://github.com/microsoft/Microsoft365DSC/blob/7b77a042bd1c8b3c6e6eb3cf11332fa265e704bd/Modules/Microsoft365DSC/Modules/M365DSCPermissions.psm1#L904-L905
https://github.com/microsoft/Microsoft365DSC/blob/7b77a042bd1c8b3c6e6eb3cf11332fa265e704bd/Modules/Microsoft365DSC/Modules/M365DSCPermissions.psm1#L1092 (this seems to be from 8 months ago)
So none of the examples given here work anymore: https://microsoft365dsc.com/user-guide/cmdlets/Update-M365DSCAzureAdApplication/#examples
eg.
Update-M365DSCAzureAdApplication -ApplicationName $Microsoft365DSC -Permissions $((Get-M365DSCCompiledPermissionList -ResourceNameList (Get-M365DSCAllResources) -PermissionType Application -AccessType Read).Permissions) -Type Certificate -CreateSelfSignedCertificate -AdminConsent -MonthsValid 12 -Credential $creds -CertificatePath c:\Temp\M365DSC.cer
would need to be without .permissions
Update-M365DSCAzureAdApplication -ApplicationName $Microsoft365DSC -Permissions $(Get-M365DSCCompiledPermissionList -ResourceNameList (Get-M365DSCAllResources) -PermissionType Application -AccessType Read) -Type Certificate -CreateSelfSignedCertificate -AdminConsent -MonthsValid 12 -Credential $creds -CertificatePath c:\Temp\M365DSC.cer
OR
Update-M365DSCAzureAdApplication -ApplicationName 'Microsoft365DSC' -Permissions @(@{Api='Graph';PermissionName='Domain.Read.All'}) -AdminConsent -Credential $creds -Type Certificate -CreateSelfSignedCertificate -CertificatePath c:\Temp\M365DSC.cer
be like this (which seems to be very counterintuitive)
Update-M365DSCAzureAdApplication -ApplicationName 'Microsoft365DSC' -Permissions @{ permissions = @(@{Api='Graph';PermissionName='Domain.Read.All'}) } -AdminConsent -Credential $creds -Type Certificate -CreateSelfSignedCertificate -CertificatePath c:\Temp\M365DSC.cer
Thanks for the detailed issue description. Will take a look at it.
@fabientschanz I'm having the hardest time to understand what is happening here, big headache today... but a quick fix is probably this
diff --git a/Modules/Microsoft365DSC/Modules/M365DSCPermissions.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCPermissions.psm1
index a79605e3e..1c3b2c277 100644
--- a/Modules/Microsoft365DSC/Modules/M365DSCPermissions.psm1
+++ b/Modules/Microsoft365DSC/Modules/M365DSCPermissions.psm1
@@ -417,8 +417,8 @@ function Get-M365DSCCompiledPermissionList
$results = @{
AdministrativeRoles = $results.AdministrativeRoles.$AccessType
Permissions = $resultsByType
- RequiredRoles = $results.RequiredRoles | Select-Object -ExpandProperty $AccessType
- RequiredRoleGroups = $results.RequiredRoleGroups | Select-Object -ExpandProperty $AccessType
+ RequiredRoles = $results.RequiredRoles.$AccessType
+ RequiredRoleGroups = $results.RequiredRoleGroups.$AccessType
}
}
Jep, that's going to be it. It works in 90% of the cases, but sometimes dynamic property fetching just doesn't. Will open a PR later this afternoon or evening. If Nik is fast enough with merging of the PRs, it might make it for the next release.
Edit: That's the nature we're dealing with in PowerShell, not-statically typed objects can introduce pitfalls 🫠
Attached the fix to an existing PR which should hopefully be merged soon. Thanks a lot for pointing it out @ssoabx, especially the one with Update-M365DSCAzureAdApplication. I don't use it that often and didn't carefully enough check it. Sorry about that.