@@ -20,19 +20,20 @@ function Get-CIPPStandards {
2020 $Table = Get-CippTable - tablename ' templates'
2121 $Filter = " PartitionKey eq 'StandardsTemplateV2'"
2222 $Templates = (Get-CIPPAzDataTableEntity @Table - Filter $Filter | Sort-Object TimeStamp).JSON |
23- ForEach-Object {
24- try {
25- # Fix old "Action" => "action"
26- $JSON = $_ -replace ' "Action":' , ' "action":' -replace ' "permissionlevel":' , ' "permissionLevel":'
27- ConvertFrom-Json - InputObject $JSON - ErrorAction SilentlyContinue
28- } catch {}
29- } |
30- Where-Object {
31- $_.GUID -like $TemplateId -and $_.runManually -eq $runManually
32- }
23+ ForEach-Object {
24+ try {
25+ # Fix old "Action" => "action"
26+ $JSON = $_ -replace ' "Action":' , ' "action":' -replace ' "permissionlevel":' , ' "permissionLevel":'
27+ ConvertFrom-Json - InputObject $JSON - ErrorAction SilentlyContinue
28+ } catch {}
29+ } |
30+ Where-Object {
31+ $_.GUID -like $TemplateId -and $_.runManually -eq $runManually
32+ }
3333
3434 # 1.5. Expand templates that contain TemplateList-Tags into multiple standards
3535 $ExpandedTemplates = foreach ($Template in $Templates ) {
36+ Write-Information " Template $ ( $Template.templateName ) ($ ( $Template.GUID ) ) processing..."
3637 $NewTemplate = $Template.PSObject.Copy ()
3738 $ExpandedStandards = [ordered ]@ {}
3839 $HasExpansions = $false
@@ -42,8 +43,7 @@ function Get-CIPPStandards {
4243 $IsArray = $StandardValue -is [System.Collections.IEnumerable ] -and -not ($StandardValue -is [string ])
4344
4445 if ($IsArray ) {
45- $NewArray = @ ()
46- foreach ($Item in $StandardValue ) {
46+ $NewArray = foreach ($Item in $StandardValue ) {
4747 if ($Item .' TemplateList-Tags' .value) {
4848 $HasExpansions = $true
4949 $Table = Get-CippTable - tablename ' templates'
@@ -54,13 +54,15 @@ function Get-CIPPStandards {
5454 $NewItem = $Item.PSObject.Copy ()
5555 $NewItem.PSObject.Properties.Remove (' TemplateList-Tags' )
5656 $NewItem | Add-Member - NotePropertyName TemplateList - NotePropertyValue ([pscustomobject ]@ {
57- label = " $ ( $TemplateItem.RowKey ) "
58- value = " $ ( $TemplateItem.RowKey ) "
59- }) - Force
60- $NewArray = $NewArray + $NewItem
57+ label = " $ ( $TemplateItem.RowKey ) "
58+ value = " $ ( $TemplateItem.RowKey ) "
59+ }) - Force
60+ $NewItem | Add-Member - NotePropertyName TemplateId - NotePropertyValue $Template.GUID - Force
61+ $NewItem
6162 }
6263 } else {
63- $NewArray = $NewArray + $Item
64+ $Item | Add-Member - NotePropertyName TemplateId - NotePropertyValue $Template.GUID - Force
65+ $Item
6466 }
6567 }
6668 $ExpandedStandards [$StandardName ] = $NewArray
@@ -71,18 +73,19 @@ function Get-CIPPStandards {
7173 $Filter = " PartitionKey eq 'IntuneTemplate'"
7274 $TemplatesList = Get-CIPPAzDataTableEntity @Table - Filter $Filter | Where-Object - Property package -EQ $StandardValue .' TemplateList-Tags' .value
7375
74- $NewArray = @ ()
75- foreach ($TemplateItem in $TemplatesList ) {
76+ $NewArray = foreach ($TemplateItem in $TemplatesList ) {
7677 $NewItem = $StandardValue.PSObject.Copy ()
7778 $NewItem.PSObject.Properties.Remove (' TemplateList-Tags' )
7879 $NewItem | Add-Member - NotePropertyName TemplateList - NotePropertyValue ([pscustomobject ]@ {
79- label = " $ ( $TemplateItem.RowKey ) "
80- value = " $ ( $TemplateItem.RowKey ) "
81- }) - Force
82- $NewArray = $NewArray + $NewItem
80+ label = " $ ( $TemplateItem.RowKey ) "
81+ value = " $ ( $TemplateItem.RowKey ) "
82+ }) - Force
83+ $NewItem | Add-Member - NotePropertyName TemplateId - NotePropertyValue $Template.GUID - Force
84+ $NewItem
8385 }
8486 $ExpandedStandards [$StandardName ] = $NewArray
8587 } else {
88+ $StandardValue | Add-Member - NotePropertyName TemplateId - NotePropertyValue $Template.GUID - Force
8689 $ExpandedStandards [$StandardName ] = $StandardValue
8790 }
8891 }
@@ -191,6 +194,13 @@ function Get-CIPPStandards {
191194 foreach ($Standard in $ComputedStandards.Keys ) {
192195 $TempCopy = $ComputedStandards [$Standard ].PSObject.Copy()
193196
197+ # Preserve TemplateId(s) before removing them from the Settings
198+ $PreservedTemplateIds = if ($TempCopy -is [System.Collections.IEnumerable ] -and -not ($TempCopy -is [string ])) {
199+ $TempCopy | ForEach-Object { $_.TemplateId }
200+ } else {
201+ $TempCopy.TemplateId
202+ }
203+
194204 # Remove 'TemplateId' from final output
195205 if ($TempCopy -is [System.Collections.IEnumerable ] -and -not ($TempCopy -is [string ])) {
196206 foreach ($subItem in $TempCopy ) {
@@ -206,12 +216,7 @@ function Get-CIPPStandards {
206216 Tenant = ' AllTenants'
207217 Standard = $Standard
208218 Settings = $Normalized
209- TemplateId = if ($ComputedStandards [$Standard ] -is [System.Collections.IEnumerable ] -and -not ($ComputedStandards [$Standard ] -is [string ])) {
210- # If multiple items from multiple templates, you may have multiple TemplateIds
211- $ComputedStandards [$Standard ] | ForEach-Object { $_.TemplateId }
212- } else {
213- $ComputedStandards [$Standard ].TemplateId
214- }
219+ TemplateId = $PreservedTemplateIds
215220 }
216221 }
217222 } else {
@@ -416,6 +421,14 @@ function Get-CIPPStandards {
416421 # 4c. Output each final standard for this tenant
417422 foreach ($Standard in $ComputedStandards.Keys ) {
418423 $TempCopy = $ComputedStandards [$Standard ].PSObject.Copy()
424+
425+ # Preserve TemplateId(s) before removing them from the Settings
426+ $PreservedTemplateIds = if ($TempCopy -is [System.Collections.IEnumerable ] -and -not ($TempCopy -is [string ])) {
427+ $TempCopy | ForEach-Object { $_.TemplateId }
428+ } else {
429+ $TempCopy.TemplateId
430+ }
431+
419432 # Remove local 'TemplateId' from final object(s)
420433 if ($TempCopy -is [System.Collections.IEnumerable ] -and -not ($TempCopy -is [string ])) {
421434 foreach ($subItem in $TempCopy ) {
@@ -431,7 +444,7 @@ function Get-CIPPStandards {
431444 Tenant = $TenantName
432445 Standard = $Standard
433446 Settings = $Normalized
434- TemplateId = $ComputedStandards [ $Standard ].TemplateId
447+ TemplateId = $PreservedTemplateIds
435448 }
436449 }
437450 }
0 commit comments