@@ -13,97 +13,130 @@ function Import-CommunityTemplate {
1313
1414 $Table = Get-CippTable - TableName ' templates'
1515
16+ try {
17+ if ($Template.RowKey ) {
18+ Write-Host " This is going to be a direct write to table, it's a CIPP template. We're writing $ ( $Template.RowKey ) "
19+ $Template = $Template | Select-Object * - ExcludeProperty Timestamp
1620
17- if ( $Template .RowKey ) {
18- Write-Host " This is going to be a direct write to table, it's a CIPP template. We're writing $ ( $ Template.RowKey ) "
19- $Template = $Template | Select-Object * - ExcludeProperty timestamp
20- Add-CIPPAzDataTableEntity @Table - Entity $Template - Force
21- } else {
22- if ( $Template .mailNickname ) { $Type = ' Group ' }
23- if ( $Template . ' @odata.type ' -like ' *conditionalAccessPolicy* ' ) { $Type = ' ConditionalAccessPolicy ' }
24- Write-Host " The type is $Type "
25- switch - Wildcard ( $Type ) {
21+ # Support both objects and json string in repo (support pretty printed json in repo)
22+ if ( Test-Json $ Template.JSON - ErrorAction SilentlyContinue) {
23+ $NewJSON = $Template.JSON | ConvertFrom-Json
24+ } else {
25+ $NewJSON = $Template .JSON
26+ }
27+
28+ # Check for existing object
29+ $Existing = Get-CIPPAzDataTableEntity @Table - Filter " RowKey eq ' $ ( $Template .RowKey ) ' and PartitionKey eq ' $ ( $Template .PartitionKey ) ' " - ErrorAction SilentlyContinue
2630
27- ' *Group*' {
28- $RawJsonObj = [PSCustomObject ]@ {
29- Displayname = $Template.displayName
30- Description = $Template.Description
31- MembershipRules = $Template.membershipRule
32- username = $Template.mailNickname
33- GUID = $Template.id
34- groupType = ' generic'
35- } | ConvertTo-Json - Depth 100
36- $entity = @ {
37- JSON = " $RawJsonObj "
38- PartitionKey = ' GroupTemplate'
39- SHA = $SHA
40- GUID = $Template.id
41- RowKey = $Template.id
31+ if ($Existing ) {
32+ if ($Existing.PartitionKey -eq ' StandardsTemplateV2' ) {
33+ # Convert existing JSON to object for updates
34+ if (Test-Json $Existing.JSON - ErrorAction SilentlyContinue) {
35+ $ExistingJSON = $Existing.JSON | ConvertFrom-Json
36+ } else {
37+ $ExistingJSON = $Existing.JSON
38+ }
39+ # Extract existing tenantFilter and excludedTenants
40+ $tenantFilter = $ExistingJSON.tenantFilter
41+ $excludedTenants = $ExistingJSON.excludedTenants
42+ $NewJSON.tenantFilter = $tenantFilter
43+ $NewJSON.excludedTenants = $excludedTenants
4244 }
43- Add-CIPPAzDataTableEntity @Table - Entity $entity - Force
44- break
4545 }
46- ' *conditionalAccessPolicy*' {
47- Write-Host $MigrationTable
48- $Template = ([pscustomobject ]$Template ) | ForEach-Object {
49- $NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object - ExpandProperty Name
50- $_ | Select-Object - Property $NonEmptyProperties
51- }
52- $id = $Template.id
53- $Template = $Template | Select-Object * - ExcludeProperty lastModifiedDateTime
, ' assignments' , ' #microsoft*' , ' *@odata.navigationLink' , ' *@odata.associationLink' , ' *@odata.context' , ' ScopeTagIds' , ' supportsScopeTags' , ' createdDateTime' , ' @odata.id' , ' @odata.editLink' , ' *odata.type' , ' [email protected] ' , createdDateTime
, ' [email protected] ' 54- Remove-ODataProperties - Object $Template
55- $RawJson = ConvertTo-Json - InputObject $Template - Depth 100 - Compress
56- # Replace the ids with the displayname by using the migration table, this is a simple find and replace each instance in the JSON.
57- $MigrationTable.objects | ForEach-Object {
58- if ($RawJson -match $_.ID ) {
59- $RawJson = $RawJson.Replace ($_.ID , $ ($_.DisplayName ))
46+ # Re-compress JSON and save to table
47+ $NewJSON = [string ]($NewJSON | ConvertTo-Json - Depth 100 - Compress)
48+ $Template.JSON = $NewJSON
49+ $Template | Add-Member - MemberType NoteProperty - Name SHA - Value $SHA - Force
50+ Add-CIPPAzDataTableEntity @Table - Entity $Template - Force
51+ } else {
52+ if ($Template.mailNickname ) { $Type = ' Group' }
53+ if ($Template .' @odata.type' -like ' *conditionalAccessPolicy*' ) { $Type = ' ConditionalAccessPolicy' }
54+ Write-Host " The type is $Type "
55+ switch - Wildcard ($Type ) {
56+
57+ ' *Group*' {
58+ $RawJsonObj = [PSCustomObject ]@ {
59+ Displayname = $Template.displayName
60+ Description = $Template.Description
61+ MembershipRules = $Template.membershipRule
62+ username = $Template.mailNickname
63+ GUID = $Template.id
64+ groupType = ' generic'
65+ } | ConvertTo-Json - Depth 100
66+ $entity = @ {
67+ JSON = " $RawJsonObj "
68+ PartitionKey = ' GroupTemplate'
69+ SHA = $SHA
70+ GUID = $Template.id
71+ RowKey = $Template.id
6072 }
73+ Add-CIPPAzDataTableEntity @Table - Entity $entity - Force
74+ break
6175 }
62- $entity = @ {
63- JSON = " $RawJson "
64- PartitionKey = ' CATemplate'
65- SHA = $SHA
66- GUID = $ID
67- RowKey = $ID
68- }
69- Add-CIPPAzDataTableEntity @Table - Entity $entity - Force
70- break
71- }
72- default {
73- $URLName = switch - Wildcard ($Template .' @odata.id' ) {
74- ' *CompliancePolicies*' { ' DeviceCompliancePolicies' }
75- ' *deviceConfigurations*' { ' Device' }
76- ' *DriverUpdateProfiles*' { ' windowsDriverUpdateProfiles' }
77- ' *SettingsCatalog*' { ' Catalog' }
78- ' *configurationPolicies*' { ' Catalog' }
79- ' *managedAppPolicies*' { ' AppProtection' }
80- ' *deviceAppManagement*' { ' AppProtection' }
76+ ' *conditionalAccessPolicy*' {
77+ Write-Host $MigrationTable
78+ $Template = ([pscustomobject ]$Template ) | ForEach-Object {
79+ $NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object - ExpandProperty Name
80+ $_ | Select-Object - Property $NonEmptyProperties
81+ }
82+ $id = $Template.id
83+ $Template = $Template | Select-Object * - ExcludeProperty lastModifiedDateTime
, ' assignments' , ' #microsoft*' , ' *@odata.navigationLink' , ' *@odata.associationLink' , ' *@odata.context' , ' ScopeTagIds' , ' supportsScopeTags' , ' createdDateTime' , ' @odata.id' , ' @odata.editLink' , ' *odata.type' , ' [email protected] ' , createdDateTime
, ' [email protected] ' 84+ Remove-ODataProperties - Object $Template
85+ $RawJson = ConvertTo-Json - InputObject $Template - Depth 100 - Compress
86+ # Replace the ids with the displayname by using the migration table, this is a simple find and replace each instance in the JSON.
87+ $MigrationTable.objects | ForEach-Object {
88+ if ($RawJson -match $_.ID ) {
89+ $RawJson = $RawJson.Replace ($_.ID , $ ($_.DisplayName ))
90+ }
91+ }
92+ $entity = @ {
93+ JSON = " $RawJson "
94+ PartitionKey = ' CATemplate'
95+ SHA = $SHA
96+ GUID = $ID
97+ RowKey = $ID
98+ }
99+ Add-CIPPAzDataTableEntity @Table - Entity $entity - Force
100+ break
81101 }
82- $id = $Template.id
83- $RawJson = $Template | Select-Object * - ExcludeProperty id
, lastModifiedDateTime
, ' assignments' , ' #microsoft*' , ' *@odata.navigationLink' , ' *@odata.associationLink' , ' *@odata.context' , ' ScopeTagIds' , ' supportsScopeTags' , ' createdDateTime' , ' @odata.id' , ' @odata.editLink' , ' [email protected] ' , ' [email protected] ' , createdDateTime
, ' [email protected] ' 84- Remove-ODataProperties - Object $RawJson
85- $RawJson = $RawJson | ConvertTo-Json - Depth 100 - Compress
102+ default {
103+ $URLName = switch - Wildcard ($Template .' @odata.id' ) {
104+ ' *CompliancePolicies*' { ' DeviceCompliancePolicies' }
105+ ' *deviceConfigurations*' { ' Device' }
106+ ' *DriverUpdateProfiles*' { ' windowsDriverUpdateProfiles' }
107+ ' *SettingsCatalog*' { ' Catalog' }
108+ ' *configurationPolicies*' { ' Catalog' }
109+ ' *managedAppPolicies*' { ' AppProtection' }
110+ ' *deviceAppManagement*' { ' AppProtection' }
111+ }
112+ $id = $Template.id
113+ $RawJson = $Template | Select-Object * - ExcludeProperty id
, lastModifiedDateTime
, ' assignments' , ' #microsoft*' , ' *@odata.navigationLink' , ' *@odata.associationLink' , ' *@odata.context' , ' ScopeTagIds' , ' supportsScopeTags' , ' createdDateTime' , ' @odata.id' , ' @odata.editLink' , ' [email protected] ' , ' [email protected] ' , createdDateTime
, ' [email protected] ' 114+ Remove-ODataProperties - Object $RawJson
115+ $RawJson = $RawJson | ConvertTo-Json - Depth 100 - Compress
86116
87- # create a new template
88- $RawJsonObj = [PSCustomObject ]@ {
89- Displayname = $Template.displayName ?? $template.Name
90- Description = $Template.Description
91- RAWJson = $RawJson
92- Type = $URLName
93- GUID = $ID
94- } | ConvertTo-Json - Depth 100 - Compress
117+ # create a new template
118+ $RawJsonObj = [PSCustomObject ]@ {
119+ Displayname = $Template.displayName ?? $template.Name
120+ Description = $Template.Description
121+ RAWJson = $RawJson
122+ Type = $URLName
123+ GUID = $ID
124+ } | ConvertTo-Json - Depth 100 - Compress
95125
96- $entity = @ {
97- JSON = " $RawJsonObj "
98- PartitionKey = ' IntuneTemplate'
99- SHA = $SHA
100- GUID = $ID
101- RowKey = $ID
102- }
103- Add-CIPPAzDataTableEntity @Table - Entity $entity - Force
126+ $entity = @ {
127+ JSON = " $RawJsonObj "
128+ PartitionKey = ' IntuneTemplate'
129+ SHA = $SHA
130+ GUID = $ID
131+ RowKey = $ID
132+ }
133+ Add-CIPPAzDataTableEntity @Table - Entity $entity - Force
104134
135+ }
105136 }
106137 }
138+ } catch {
139+ Write-Warning " Community template import failed. Error: $ ( $_.Exception.Message ) "
140+ Write-Information $_.InvocationInfo.PositionMessage
107141 }
108-
109142}
0 commit comments