diff --git a/CHANGELOG.md b/CHANGELOG.md index 065a64ee63..52b4377653 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ * Removed verbose output from `Get-TargetResource`. * Updated the error behavior to always throw inside `Get-TargetResource`. +* MSFT_IntuneSettingCatalogCustomPolicyWindows10 + * Fixed the ability to run Get-TargetResource via the LCM (Get-DscConfiguration) and it's ability to return complex nested objects. + FIXES [#6092](https://github.com/microsoft/Microsoft365DSC/issues/6092) + # 1.25.1203.2 * DEPENDENCIES diff --git a/Modules/Microsoft365DSC/DSCResources/MSFT_IntuneSettingCatalogCustomPolicyWindows10/MSFT_IntuneSettingCatalogCustomPolicyWindows10.psm1 b/Modules/Microsoft365DSC/DSCResources/MSFT_IntuneSettingCatalogCustomPolicyWindows10/MSFT_IntuneSettingCatalogCustomPolicyWindows10.psm1 index b1eefa4a8e..95528510bc 100644 --- a/Modules/Microsoft365DSC/DSCResources/MSFT_IntuneSettingCatalogCustomPolicyWindows10/MSFT_IntuneSettingCatalogCustomPolicyWindows10.psm1 +++ b/Modules/Microsoft365DSC/DSCResources/MSFT_IntuneSettingCatalogCustomPolicyWindows10/MSFT_IntuneSettingCatalogCustomPolicyWindows10.psm1 @@ -142,21 +142,37 @@ function Get-TargetResource $complexSettings = @() foreach ($currentSettings in $getValue.settings) { - $mySettings = [ordered]@{} - $complexSettingInstance = [ordered]@{} - $complexSettingInstance.Add('SettingDefinitionId', $currentSettings.settingInstance.settingDefinitionId) - $complexSettingInstance.Add('odataType', $currentSettings.settingInstance.AdditionalProperties.'@odata.type') - $valueName = $currentSettings.settingInstance.AdditionalProperties.'@odata.type'.Replace('#microsoft.graph.deviceManagementConfiguration', '').Replace('Instance', 'Value') - $valueName = Get-StringFirstCharacterToLower -Value $valueName + $complexSettingInstance = [hashtable]@{} + if ( -Not([string]::IsNullOrEmpty($currentSettings.SettingInstance.AdditionalProperties.'@odata.type')) ) + { + $complexSettingInstance['odataType'] = $currentSettings.SettingInstance.AdditionalProperties.'@odata.type' + } + if ( -Not([string]::IsNullOrEmpty($currentSettings.SettingInstance.settingDefinitionId)) ) + { + $complexSettingInstance['SettingDefinitionId'] = $currentSettings.settingInstance.settingDefinitionId + } + if ( -Not([string]::IsNullOrEmpty($currentSettings.settingInstance.SettingInstanceTemplateReference.SettingInstanceTemplateId)) ) + { + $complexSettingInstance['SettingInstanceTemplateReference'] = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingInstanceTemplateReference -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property @{ + 'SettingInstanceTemplateId' = "$($currentSettings.settingInstance.SettingInstanceTemplateReference.SettingInstanceTemplateId)" + } -ClientOnly + } + $valueName = $currentSettings.settingInstance.AdditionalProperties.keys | Where-Object { @('ChoiceSettingCollectionValue','ChoiceSettingValue','GroupSettingCollectionValue','GroupSettingValue','SimpleSettingCollectionValue','SimpleSettingValue') -contains $_ } $rawValue = $currentSettings.settingInstance.AdditionalProperties.$valueName - $complexValue = get-SettingValue -SettingValue $rawValue -SettingValueType $currentSettings.settingInstance.AdditionalProperties.'@odata.type' - $complexSettingInstance.Add($valueName, $complexValue) - $mySettings.Add('SettingInstance', $complexSettingInstance) - if ($mySettings.values.Where({ $null -ne $_ }).Count -gt 0) + $complexValue = Get-SettingValue -SettingValue $rawValue -SettingValueType $currentSettings.settingInstance.AdditionalProperties.'@odata.type' + if ( ('ChoiceSettingCollectionValue','GroupSettingCollectionValue','SimpleSettingCollectionValue') -contains $valueName ) { - $complexSettings += $mySettings + $complexSettingInstance[$valueName] = [CimInstance[]]$complexValue } + else + { + $complexSettingInstance[$valueName] = $complexValue + } + $complexSettings += New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSetting -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property @{ + 'SettingInstance' = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingInstance -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $complexSettingInstance -ClientOnly + } -ClientOnly } + #region resource generator code $enumPlatforms = $null if ($null -ne $getValue.Platforms) @@ -703,9 +719,8 @@ function Export-TargetResource function Get-SettingValue { [CmdletBinding()] - [OutputType([System.Collections.Hashtable], [System.Collections.Hashtable[]])] - param - ( + [OutputType([CimInstance[]], [System.Collections.Hashtable], [System.Collections.Hashtable[]])] + param ( [Parameter()] $SettingValue, [Parameter()] @@ -717,134 +732,210 @@ function Get-SettingValue { '*ChoiceSettingInstance' { - $complexValue = [ordered]@{} - $complexValue.Add('odataType', $SettingValue.'@odata.type') - $complexValue.Add('Value', $SettingValue.value) - $children = @() - foreach ($child in $SettingValue.children) + $hash = [hashtable]@{} + if ($SettingValue.Keys -contains '@odata.type' -and -Not([string]::IsNullOrEmpty($SettingValue.'@odata.type')) ) { - $complexChild = [ordered]@{} - $complexChild.Add('SettingDefinitionId', $child.settingDefinitionId) - $complexChild.Add('odataType', $child.'@odata.type') - $valueName = $child.'@odata.type'.Replace('#microsoft.graph.deviceManagementConfiguration', '').Replace('Instance', 'Value') - $valueName = Get-StringFirstCharacterToLower -Value $valueName - $rawValue = $child.$valueName - $childSettingValue = Get-SettingValue -SettingValue $rawValue -SettingValueType $child.'@odata.type' - $complexChild.Add($valueName, $childSettingValue) - $children += $complexChild + $hash['odataType'] = $SettingValue.'@odata.type' } - $complexValue.Add('Children', $children) - } - '*ChoiceSettingCollectionInstance' - { - $complexCollection = @() - foreach ($item in $SettingValue) + if ($SettingValue.Keys -contains 'value' -and -Not([string]::IsNullOrEmpty($SettingValue.value)) ) + { + $hash['Value'] = $SettingValue.value + } + if ($SettingValue.Keys -contains 'SettingValueTemplateReference') + { + if ( -Not [string]::IsNullOrEmpty($SettingValue.SettingValueTemplateReference.SettingInstanceTemplateId) ) + { + $hash['SettingValueTemplateReference'] = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingValueTemplateReference -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property @{ + 'SettingInstanceTemplateId' = $SettingValue.SettingValueTemplateReference.SettingInstanceTemplateId + } -ClientOnly + } + } + if ( -Not [String]::IsNullOrEmpty($SettingValue.children) ) { - $complexValue = [ordered]@{} - $complexValue.Add('Value', $item.value) $children = @() - foreach ($child in $item.children) + foreach ($child in $SettingValue.children) { - $complexChild = [ordered]@{} - $complexChild.Add('SettingDefinitionId', $child.settingDefinitionId) - $complexChild.Add('odataType', $child.'@odata.type') - $valueName = $child.'@odata.type'.Replace('#microsoft.graph.deviceManagementConfiguration', '').Replace('Instance', 'Value') - $valueName = Get-StringFirstCharacterToLower -Value $valueName + $childHash = [hashtable]@{} + if ( -Not([string]::IsNullOrEmpty($child.'@odata.type')) ) + { + $childHash['odataType'] = $child.'@odata.type' + } + if ( -Not([string]::IsNullOrEmpty($child.settingDefinitionId)) ) + { + $childHash['SettingDefinitionId'] = $child.settingDefinitionId + } + if ( -Not [string]::IsNullOrEmpty($child.SettingValueTemplateReference.SettingInstanceTemplateId) ) + { + $childHash['SettingValueTemplateReference'] = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingInstanceTemplateReference -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $child.SettingValueTemplateReference -ClientOnly + } + $valueName = $child.keys | Where-Object { @('ChoiceSettingCollectionValue','ChoiceSettingValue','GroupSettingCollectionValue','GroupSettingValue','SimpleSettingCollectionValue','SimpleSettingValue') -contains $_ } $rawValue = $child.$valueName $childSettingValue = Get-SettingValue -SettingValue $rawValue -SettingValueType $child.'@odata.type' - $complexChild.Add($valueName, $childSettingValue) + if ( ('ChoiceSettingCollectionValue','GroupSettingCollectionValue','SimpleSettingCollectionValue') -contains $valueName ) + { + $childHash.Add( $valueName, [CimInstance[]]$childSettingValue ) + } + else + { + $childHash.Add( $valueName, $childSettingValue ) + } + $complexChild = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingInstance -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $childHash -ClientOnly $children += $complexChild } - $complexValue.Add('Children', $children) - $complexCollection += $complexValue + $hash['Children'] = [CimInstance[]]($Children) } - return , ([hashtable[]]$complexCollection) + return (New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationChoiceSettingValue -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $hash -ClientOnly) + } + '*ChoiceSettingCollectionInstance' + { + $complexCollection = @() + foreach ($item in $SettingValue) + { + $complexCollection += Get-SettingValue -SettingValue $item -SettingValueType '#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance' + } + return [CimInstance[]]($complexCollection) } '*SimpleSettingInstance' { - $complexValue = [ordered]@{} - $complexValue.Add('odataType', $SettingValue.'@odata.type') - $valueName = 'IntValue' - $value = $SettingValue.value - if ($SettingValue.'@odata.type' -ne '#microsoft.graph.deviceManagementConfigurationIntegerSettingValue') + $hash = [hashtable]@{} + If($SettingValue.Keys -contains '@odata.type' -and -Not([string]::IsNullOrEmpty($SettingValue.'@odata.type')) ) { - $valueName = 'StringValue' + $hash['odataType'] = $SettingValue.'@odata.type' } - $complexValue.Add($valueName, $value) - if ($SettingValue.'@odata.type' -eq '#microsoft.graph.deviceManagementConfigurationSecretSettingValue') + if ($SettingValue.Keys -contains 'value' -and -Not([string]::IsNullOrEmpty($SettingValue.value)) ) { - $complexValue.Add('ValueState', $SettingValue.valueState) + try + { + $hash['IntValue'] = [UInt32]($SettingValue.value) + } + catch { + $hash['StringValue'] = [string]($SettingValue.value) + } } - } - '*SimpleSettingCollectionInstance' - { - $complexCollection = @() - - foreach ($item in $SettingValue) + if ($SettingValue.Keys -contains 'ValueState' -and -Not([string]::IsNullOrEmpty($SettingValue.ValueState)) ) + { + $hash['ValueState'] = $SettingValue.ValueState + } + if ($SettingValue.Keys -contains 'SettingValueTemplateReference') { - $complexValue = [ordered]@{} - $complexValue.Add('odataType', $item.'@odata.type') - $valueName = 'IntValue' - $value = $item.value - if ($item.'@odata.type' -ne '#microsoft.graph.deviceManagementConfigurationIntegerSettingValue') + if ( -Not [string]::IsNullOrEmpty($SettingValue.SettingValueTemplateReference.SettingInstanceTemplateId) ) { - $valueName = 'StringValue' + $hash['SettingValueTemplateReference'] = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingValueTemplateReference -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property @{ + 'SettingInstanceTemplateId' = $SettingValue.SettingValueTemplateReference.SettingInstanceTemplateId + } -ClientOnly } - $complexValue.Add($valueName, $value) - if ($item.'@odata.type' -eq '#microsoft.graph.deviceManagementConfigurationSecretSettingValue') + } + if ( -Not [String]::IsNullOrEmpty($SettingValue.children) ) + { + $children = @() + foreach ($child in $SettingValue.children) { - $complexValue.Add('ValueState', $item.valueState) + $childHash = [hashtable]@{} + if ( -Not([string]::IsNullOrEmpty($child.'@odata.type')) ) + { + $childHash['odataType'] = $child.'@odata.type' + } + if ( -Not([string]::IsNullOrEmpty($child.settingDefinitionId)) ) + { + $childHash['SettingDefinitionId'] = $child.settingDefinitionId + } + if ( -Not [string]::IsNullOrEmpty($child.SettingValueTemplateReference.SettingInstanceTemplateId) ) + { + $childHash['SettingValueTemplateReference'] = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingInstanceTemplateReference -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $child.SettingValueTemplateReference -ClientOnly + } + $valueName = $child.keys | Where-Object { @('ChoiceSettingCollectionValue','ChoiceSettingValue','GroupSettingCollectionValue','GroupSettingValue','SimpleSettingCollectionValue','SimpleSettingValue') -contains $_ } + $rawValue = $child.$valueName + $childSettingValue = Get-SettingValue -SettingValue $rawValue -SettingValueType $child.'@odata.type' + if ( ('ChoiceSettingCollectionValue','GroupSettingCollectionValue','SimpleSettingCollectionValue') -contains $valueName ) + { + $childHash.Add( $valueName, [CimInstance[]]$childSettingValue ) + } + else { + $childHash.Add( $valueName, $childSettingValue ) + } + $complexChild = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingInstance -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $childHash -ClientOnly + $children += $complexChild } - $complexCollection += $complexValue + $hash['Children'] = [CimInstance[]]($Children) } - return , ([hashtable[]]$complexCollection) + return (New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSimpleSettingValue -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $hash -ClientOnly) + } - '*GroupSettingInstance' + '*SimpleSettingCollectionInstance' { - $complexValue = [ordered]@{} - $complexValue.Add('odataType', $SettingValue.'@odata.type') - $children = @() - foreach ($child in $SettingValue.children) + $complexCollection = @() + foreach ($item in $SettingValue) { - $complexChild = [ordered]@{} - $complexChild.Add('SettingDefinitionId', $child.settingDefinitionId) - $complexChild.Add('odataType', $child.'@odata.type') - $valueName = $child.'@odata.type'.Replace('#microsoft.graph.deviceManagementConfiguration', '').Replace('Instance', 'Value') - $valueName = Get-StringFirstCharacterToLower -Value $valueName - $rawValue = $child.$valueName - $settingValue = Get-SettingValue -SettingValue $rawValue -SettingValueType $child.'@odata.type' - $complexChild.Add($valueName, $settingValue) - $children += $complexChild + $complexCollection += Get-SettingValue -SettingValue $item -SettingValueType '#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance' } - $complexValue.Add('Children', $children) + return [CimInstance[]]($complexCollection) } - '*GroupSettingCollectionInstance' + '*GroupSettingInstance' { - $complexCollection = @() - foreach ($groupSettingValue in $SettingValue) + $hash = [hashtable]@{} + if ($SettingValue.Keys -contains '@odata.type' -and -Not([string]::IsNullOrEmpty($SettingValue.'@odata.type')) ) + { + $hash['odataType'] = $SettingValue.'@odata.type' + } + if ($SettingValue.Keys -contains 'value' -and -Not([string]::IsNullOrEmpty($SettingValue.value)) ) + { + $hash['Value'] = $SettingValue.value + } + if ($SettingValue.Keys -contains 'SettingValueTemplateReference') + { + if ( -Not [string]::IsNullOrEmpty($SettingValue.SettingValueTemplateReference.SettingInstanceTemplateId) ) + { + $hash['SettingValueTemplateReference'] = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingValueTemplateReference -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property @{ + 'SettingInstanceTemplateId' = $SettingValue.SettingValueTemplateReference.SettingInstanceTemplateId + } -ClientOnly + } + } + if ( -Not [String]::IsNullOrEmpty($SettingValue.children) ) { - $complexValue = [ordered]@{} - #$complexValue.Add('odataType',$SettingValue.'@odata.type') $children = @() - foreach ($child in $groupSettingValue.children) + foreach ($child in $SettingValue.children) { - $complexChild = [ordered]@{} - $complexChild.Add('SettingDefinitionId', $child.settingDefinitionId) - $complexChild.Add('odataType', $child.'@odata.type') - $valueName = $child.'@odata.type'.Replace('#microsoft.graph.deviceManagementConfiguration', '').Replace('Instance', 'Value') - $valueName = Get-StringFirstCharacterToLower -Value $valueName + $childHash = [hashtable]@{} + if ( -Not([string]::IsNullOrEmpty($child.'@odata.type')) ) + { + $childHash['odataType'] = $child.'@odata.type' + } + if ( -Not([string]::IsNullOrEmpty($child.settingDefinitionId)) ) + { + $childHash['SettingDefinitionId'] = $child.settingDefinitionId + } + if ( -Not [string]::IsNullOrEmpty($child.SettingValueTemplateReference.SettingInstanceTemplateId) ) + { + $childHash['SettingValueTemplateReference'] = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingInstanceTemplateReference -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $child.SettingValueTemplateReference -ClientOnly + } + $valueName = $child.keys | Where-Object { @('ChoiceSettingCollectionValue','ChoiceSettingValue','GroupSettingCollectionValue','GroupSettingValue','SimpleSettingCollectionValue','SimpleSettingValue') -contains $_ } $rawValue = $child.$valueName - $settingValue = Get-SettingValue -SettingValue $rawValue -SettingValueType $child.'@odata.type' - $complexChild.Add($valueName, $settingValue) + $childSettingValue = Get-SettingValue -SettingValue $rawValue -SettingValueType $child.'@odata.type' + if ( ('ChoiceSettingCollectionValue','GroupSettingCollectionValue','SimpleSettingCollectionValue') -contains $valueName ) + { + $childHash.Add( $valueName, [CimInstance[]]$childSettingValue ) + } + else + { + $childHash.Add( $valueName, $childSettingValue ) + } + $complexChild = New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationSettingInstance -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $childHash -ClientOnly $children += $complexChild } - $complexValue.Add('Children', $children) - $complexCollection += $complexValue + $hash['Children'] = [CimInstance[]]($Children) + } + return (New-CimInstance -ClassName MSFT_MicrosoftGraphDeviceManagementConfigurationGroupSettingValue -Namespace root/Microsoft/Windows/DesiredStateConfiguration -Property $hash -ClientOnly) + + } + '*GroupSettingCollectionInstance' + { + $complexCollection = @() + foreach ($item in $SettingValue) { + $complexCollection += Get-SettingValue -SettingValue $item -SettingValueType '#microsoft.graph.deviceManagementConfigurationGroupSettingInstance' } - return , ([hashtable[]]$complexCollection) + return [CimInstance[]]($complexCollection) } } - return $complexValue } function Update-IntuneDeviceConfigurationPolicy