@@ -147,19 +147,19 @@ function Get-TargetResource
147147 }
148148 }
149149
150- $ResourceScopesDisplayNames = @ ()
151- foreach ($ResourceScope in $getValue.ResourceScopes )
150+ $resourceScopesDisplayNamesValue = @ ()
151+ foreach ($resourceScope in $getValue.ResourceScopes )
152152 {
153- $group = Get-MgGroup - GroupId $ResourceScope - ErrorAction SilentlyContinue
153+ $group = Get-MgGroup - GroupId $resourceScope - ErrorAction SilentlyContinue
154154 if ($null -eq $group )
155155 {
156- Write-Warning - Message " Could not find group with Id {$ResourceScope } when retrieving resource scope display names"
156+ Write-Warning - Message " Could not find group with Id {$resourceScope } when retrieving resource scope display names"
157157 continue
158158 }
159- $ResourceScopesDisplayNames += $group.DisplayName
159+ $resourceScopesDisplayNamesValue += $group.DisplayName
160160 }
161161
162- $MembersDisplayNames = @ ()
162+ $membersDisplayNamesValue = @ ()
163163 foreach ($tempMember in $getValue.Members )
164164 {
165165 $group = Get-MgGroup - GroupId $tempMember - ErrorAction SilentlyContinue
@@ -168,7 +168,7 @@ function Get-TargetResource
168168 Write-Warning - Message " Could not find group with Id {$tempMember } when retrieving member display names"
169169 continue
170170 }
171- $MembersDisplayNames += $group.DisplayName
171+ $membersDisplayNamesValue += $group.DisplayName
172172 }
173173
174174 $scopeTypeValue = $null
@@ -181,10 +181,10 @@ function Get-TargetResource
181181 Description = $getValue.Description
182182 DisplayName = $getValue.DisplayName
183183 ResourceScopes = $getValue.ResourceScopes
184- ResourceScopesDisplayNames = $ResourceScopesDisplayNames
184+ ResourceScopesDisplayNames = $resourceScopesDisplayNamesValue
185185 ScopeType = $scopeTypeValue
186186 Members = $getValue.Members
187- MembersDisplayNames = $MembersDisplayNames
187+ MembersDisplayNames = $membersDisplayNamesValue
188188 RoleDefinition = $RoleDefinition
189189 RoleDefinitionDisplayName = $RoleDefinitionDisplayName
190190 Ensure = ' Present'
@@ -308,76 +308,93 @@ function Set-TargetResource
308308
309309 if ($RoleDefinition -notmatch ' ^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$' -or $RoleDefinition -eq ' 00000000-0000-0000-0000-000000000000' )
310310 {
311- [ string ] $roleDefinition = $null
311+ $RoleDefinition = $null
312312 $filter = " DisplayName eq '$ ( $RoleDefinitionDisplayName -replace " '" , " ''" ) '"
313313 $roleDefinitionId = Get-MgDeviceManagementRoleDefinition - All - Filter $filter - ErrorAction SilentlyContinue
314314 if ($null -ne $roleDefinitionId )
315315 {
316- $roleDefinition = $roleDefinitionId.Id
316+ $RoleDefinition = $roleDefinitionId.Id
317317 }
318318 else
319319 {
320320 Write-Verbose - Message " No role definition with DisplayName {$RoleDefinitionDisplayName } was found"
321321 }
322322 }
323323
324- [array ]$members = @ ()
325- foreach ($membersDisplayName in $ MembersDisplayNames )
324+ [array ]$membersValue = @ ()
325+ if ($PSBoundParameters .ContainsKey ( ' MembersDisplayNames' ) )
326326 {
327- $filter = " displayName eq '$ ( $membersDisplayName -replace " '" , " ''" ) '"
328- $memberId = Get-MgGroup - Filter $filter - ErrorAction SilentlyContinue
329- if ($null -ne $memberId )
327+ foreach ($membersDisplayName in $MembersDisplayNames )
330328 {
331- if ($members -notcontains $memberId.Id )
329+ $filter = " displayName eq '$ ( $membersDisplayName -replace " '" , " ''" ) '"
330+ $memberId = Get-MgGroup - Filter $filter - ErrorAction SilentlyContinue
331+ if ($null -ne $memberId )
332332 {
333- $members += $memberId.Id
333+ if ($membersValue -notcontains $memberId.Id )
334+ {
335+ $membersValue += $memberId.Id
336+ }
337+ }
338+ else
339+ {
340+ Write-Verbose - Message " No member of type group with DisplayName {$membersDisplayName } was found"
334341 }
335342 }
336- else
337- {
338- Write-Verbose - Message " No member of type group with DisplayName { $membersDisplayName } was found "
339- }
343+ }
344+ else
345+ {
346+ $membersValue = $Members
340347 }
341348
342- [array ]$resourceScopes = @ ()
343- foreach ($resourceScopesDisplayName in $ ResourceScopesDisplayNames )
349+ [array ]$resourceScopesValue = @ ()
350+ if ($PSBoundParameters .ContainsKey ( ' ResourceScopesDisplayNames' ) )
344351 {
345- $filter = " DisplayName eq '$ ( $resourceScopesDisplayName -replace " '" , " ''" ) '"
346- $resourceScopeId = Get-MgGroup - Filter $filter - ErrorAction SilentlyContinue
347- if ($null -ne $resourceScopeId )
352+ foreach ($resourceScopesDisplayName in $ResourceScopesDisplayNames )
348353 {
349- if ($ResourceScopes -notcontains $resourceScopeId.Id )
354+ $filter = " DisplayName eq '$ ( $resourceScopesDisplayName -replace " '" , " ''" ) '"
355+ $resourceScopeId = Get-MgGroup - Filter $filter - ErrorAction SilentlyContinue
356+ if ($null -ne $resourceScopeId )
350357 {
351- $ResourceScopes += $resourceScopeId.Id
358+ if ($resourceScopesValue -notcontains $resourceScopeId.Id )
359+ {
360+ $resourceScopesValue += $resourceScopeId.Id
361+ }
362+ }
363+ else
364+ {
365+ Write-Verbose - Message " No resource scope of type group with DisplayName {$resourceScopesDisplayName } was found"
352366 }
353367 }
354- else
355- {
356- Write-Verbose - Message " No resource scope of type group with DisplayName {$ResourceScopesDisplayName } was found"
357- }
358368 }
369+ else
370+ {
371+ $resourceScopesValue = $ResourceScopes
372+ }
373+
374+ $scopeTypeValue = $ScopeType
359375 if ($ScopeType -match ' AllDevices|AllLicensedUsers|AllDevicesAndLicensedUsers' )
360376 {
361- $ResourceScopes = $null
377+ $resourceScopesValue = $null
362378 }
363379 else
364380 {
365- $ScopeType = ' resourceScope'
366- $ResourceScopes = $resourceScopes
381+ $scopeTypeValue = ' resourceScope'
367382 }
383+
368384 if ($Ensure -eq ' Present' -and $currentInstance.Ensure -eq ' Absent' )
369385 {
370386 Write-Verbose - Message " Creating an Intune Role Assignment with DisplayName {$DisplayName }"
371387
372388 $CreateParameters = @ {
373389 description = $Description
374390 displayName = $DisplayName
375- resourceScopes = $ResourceScopes
376- scopeType = $ScopeType
377- members = $Members
391+ resourceScopes = $resourceScopesValue
392+ scopeType = $scopeTypeValue
393+ members = $membersValue
378394 ' @odata.type' = ' #microsoft.graph.deviceAndAppManagementRoleAssignment'
379- ' roleDefinition@odata.bind' = " $ ( (Get-MSCloudLoginConnectionProfile - Workload MicrosoftGraph).ResourceUrl) beta/deviceManagement/roleDefinitions('$roleDefinition ')"
395+ ' roleDefinition@odata.bind' = " $ ( (Get-MSCloudLoginConnectionProfile - Workload MicrosoftGraph).ResourceUrl) beta/deviceManagement/roleDefinitions('$RoleDefinition ')"
380396 }
397+
381398 $null = New-MgBetaDeviceManagementRoleAssignment - BodyParameter $CreateParameters
382399 }
383400 elseif ($Ensure -eq ' Present' -and $currentInstance.Ensure -eq ' Present' )
@@ -387,14 +404,15 @@ function Set-TargetResource
387404 $UpdateParameters = @ {
388405 description = $Description
389406 displayName = $DisplayName
390- resourceScopes = $ResourceScopes
391- scopeType = $ScopeType
392- members = $Members
407+ resourceScopes = $resourceScopesValue
408+ scopeType = $scopeTypeValue
409+ members = $membersValue
393410 ' @odata.type' = ' #microsoft.graph.deviceAndAppManagementRoleAssignment'
394- ' roleDefinition@odata.bind' = " $ ( (Get-MSCloudLoginConnectionProfile - Workload MicrosoftGraph).ResourceUrl) beta/deviceManagement/roleDefinitions('$roleDefinition ')"
411+ ' roleDefinition@odata.bind' = " $ ( (Get-MSCloudLoginConnectionProfile - Workload MicrosoftGraph).ResourceUrl) beta/deviceManagement/roleDefinitions('$RoleDefinition ')"
395412 }
396413
397- Update-MgBetaDeviceManagementRoleAssignment - BodyParameter $UpdateParameters `
414+ $null = Update-MgBetaDeviceManagementRoleAssignment `
415+ - BodyParameter $UpdateParameters `
398416 - DeviceAndAppManagementRoleAssignmentId $currentInstance.Id
399417 }
400418 elseif ($Ensure -eq ' Absent' -and $currentInstance.Ensure -eq ' Present' )
@@ -493,57 +511,6 @@ function Test-TargetResource
493511 Add-M365DSCTelemetryEvent - Data $data
494512 # endregion
495513
496- if (-not ($RoleDefinition -match ' ^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$' ))
497- {
498- [string ]$roleDefinition = $null
499- $filter = " displayName eq '$ ( $RoleDefinitionDisplayName -replace " '" , " ''" ) '"
500- $roleDefinitionId = Get-MgDeviceManagementRoleDefinition - All - Filter $filter - ErrorAction SilentlyContinue
501- if ($null -ne $roleDefinitionId )
502- {
503- $roleDefinition = $roleDefinitionId.Id
504- $PSBoundParameters.RoleDefinition = $roleDefinition
505- }
506- else
507- {
508- Write-Verbose - Message " No role definition with DisplayName {$RoleDefinitionDisplayName } was found"
509- }
510- }
511-
512- foreach ($membersDisplayName in $MembersDisplayNames )
513- {
514- $filter = " DisplayName eq '$ ( $MembersDisplayName -replace " '" , " ''" ) '"
515- $newMember = Get-MgGroup - Filter $filter - ErrorAction SilentlyContinue
516- if ($null -ne $newMember )
517- {
518- if ($Members -notcontains $newMember.Id )
519- {
520- $Members += $newMember.Id
521- }
522- }
523- else
524- {
525- Write-Verbose - Message " No member of type group with DisplayName {$membersDisplayName } was found"
526- }
527- }
528- $PSBoundParameters.Members = $Members
529-
530- foreach ($resourceScopesDisplayName in $ResourceScopesDisplayNames )
531- {
532- $filter = " displayName eq '$ ( $resourceScopesDisplayName -replace " '" , " ''" ) '"
533- $newResourceScope = Get-MgGroup - Filter $filter - ErrorAction SilentlyContinue
534- if ($null -ne $newResourceScope )
535- {
536- if ($ResourceScopes -notcontains $newResourceScope.Id )
537- {
538- $ResourceScopes += $newResourceScope.Id
539- }
540- }
541- else
542- {
543- Write-Verbose - Message " No resource scope of type group with DisplayName {$ResourceScopesDisplayName } was found"
544- }
545- }
546- $PSBoundParameters.ResourceScopes = $ResourceScopes
547514
548515 $compareParameters = Get-CompareParameters
549516 $result = Test-M365DSCTargetResource - DesiredValues $PSBoundParameters `
@@ -700,7 +667,22 @@ function Get-CompareParameters
700667 param ()
701668
702669 return @ {
703- ExcludedProperties = @ (' ResourceScopesDisplayNames' , ' MembersDisplayNames' )
670+ PostProcessing = {
671+ param ($DesiredValues , $CurrentValues , $ValuesToCheck , $ignore )
672+ if ($DesiredValues.ContainsKey (' MembersDisplayNames' ))
673+ {
674+ $ValuesToCheck.Remove (' Members' ) | Out-Null
675+ }
676+ if ($DesiredValues.ContainsKey (' ResourceScopesDisplayNames' ))
677+ {
678+ $ValuesToCheck.Remove (' ResourceScopes' ) | Out-Null
679+ }
680+ if ($DesiredValues.ContainsKey (' RoleDefinitionDisplayName' ))
681+ {
682+ $ValuesToCheck.Remove (' RoleDefinition' ) | Out-Null
683+ }
684+ return [System.Tuple [Hashtable , Hashtable , Hashtable ]]::new($DesiredValues , $CurrentValues , $ValuesToCheck )
685+ }
704686 }
705687}
706688
0 commit comments