@@ -88,16 +88,23 @@ function New-CIPPTemplateRun {
8888 Write-Information ' Creating templates for found Conditional Access Policies'
8989 foreach ($policy in $policies ) {
9090 try {
91+ $Hash = Get-StringHash - String ($policy | ConvertTo-Json - Depth 100 - Compress)
92+ $ExistingPolicy = $ExistingTemplates | Where-Object { $_.displayName -eq $policy.displayName } | Select-Object - First 1
93+ if ($ExistingPolicy -and $ExistingPolicy.SHA -eq $Hash ) {
94+ " Policy $ ( $policy.displayName ) found, SHA matches, skipping template creation"
95+ continue
96+ }
9197 $Template = New-CIPPCATemplate - TenantFilter $TenantFilter - JSON $policy
9298 # check existing templates, if the displayName is the same, overwrite it.
93- $ExistingPolicy = $ExistingTemplates | Where-Object { $_ .displayName -eq $policy .displayName } | Select-Object - First 1
99+
94100 if ($ExistingPolicy -and $ExistingPolicy.PartitionKey -eq ' CATemplate' ) {
95101 " Policy $ ( $policy.displayName ) found, updating template"
96102 Add-CIPPAzDataTableEntity @Table - Entity @ {
97103 JSON = " $Template "
98104 RowKey = $ExistingPolicy.GUID
99105 PartitionKey = ' CATemplate'
100106 GUID = $ExistingPolicy.GUID
107+ SHA = $Hash
101108 } - Force
102109 } else {
103110 " Policy $ ( $policy.displayName ) not found in existing templates, creating new template"
@@ -107,6 +114,7 @@ function New-CIPPTemplateRun {
107114 RowKey = " $GUID "
108115 PartitionKey = ' CATemplate'
109116 GUID = " $GUID "
117+ SHA = $Hash
110118 }
111119 }
112120
@@ -133,8 +141,15 @@ function New-CIPPTemplateRun {
133141 $URLName = (($url ).split(' ?' ) | Select-Object - First 1 ) -replace ' https://graph.microsoft.com/beta/deviceManagement/' , ' '
134142 foreach ($Policy in $Policies ) {
135143 try {
136- $Template = New-CIPPIntuneTemplate - TenantFilter $TenantFilter - URLName $URLName - ID $Policy .ID
144+ $Hash = Get-StringHash - String ( $Policy | ConvertTo-Json - Depth 100 - Compress)
137145 $ExistingPolicy = $ExistingTemplates | Where-Object { $_.displayName -eq $Template.DisplayName } | Select-Object - First 1
146+
147+ if ($ExistingPolicy -and $ExistingPolicy.SHA -eq $Hash ) {
148+ " Policy $ ( $Policy.displayName ) found, SHA matches, skipping template creation"
149+ continue
150+ }
151+
152+ $Template = New-CIPPIntuneTemplate - TenantFilter $TenantFilter - URLName $URLName - ID $Policy.ID
138153 if ($ExistingPolicy -and $ExistingPolicy.PartitionKey -eq ' IntuneTemplate' ) {
139154 " Policy $ ( $Template.DisplayName ) found, updating template"
140155 $object = [PSCustomObject ]@ {
@@ -149,6 +164,8 @@ function New-CIPPTemplateRun {
149164 JSON = " $object "
150165 RowKey = $ExistingPolicy.GUID
151166 PartitionKey = ' IntuneTemplate'
167+ Package = $ExistingPolicy.Package
168+ SHA = $Hash
152169 } - Force
153170 } else {
154171 " Policy $ ( $Template.DisplayName ) not found in existing templates, creating new template"
@@ -165,6 +182,7 @@ function New-CIPPTemplateRun {
165182 JSON = " $object "
166183 RowKey = " $GUID "
167184 PartitionKey = ' IntuneTemplate'
185+ SHA = $Hash
168186 } - Force
169187 }
170188 } catch {
@@ -180,8 +198,14 @@ function New-CIPPTemplateRun {
180198 ' intunecompliance' {
181199 Write-Information " Backup Intune Compliance Policies for $TenantFilter "
182200 New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies?$top=999' - tenantid $TenantFilter | ForEach-Object {
201+ $Hash = Get-StringHash - String (ConvertTo-Json - Depth 100 - Compress - InputObject $_ )
202+ $ExistingPolicy = $ExistingTemplates | Where-Object { $_.displayName -eq $_.DisplayName } | Select-Object - First 1
203+ if ($ExistingPolicy -and $ExistingPolicy.SHA -eq $Hash ) {
204+ " Policy $ ( $_.DisplayName ) found, SHA matches, skipping template creation"
205+ continue
206+ }
207+
183208 $Template = New-CIPPIntuneTemplate - TenantFilter $TenantFilter - URLName ' deviceCompliancePolicies' - ID $_.ID
184- $ExistingPolicy = $ExistingTemplates | Where-Object { $_.displayName -eq $Template.DisplayName } | Select-Object - First 1
185209 if ($ExistingPolicy -and $ExistingPolicy.PartitionKey -eq ' IntuneTemplate' ) {
186210 " Policy $ ( $Template.DisplayName ) found, updating template"
187211 $object = [PSCustomObject ]@ {
@@ -196,6 +220,8 @@ function New-CIPPTemplateRun {
196220 JSON = " $object "
197221 RowKey = $ExistingPolicy.GUID
198222 PartitionKey = ' IntuneTemplate'
223+ Package = $ExistingPolicy.Package
224+ SHA = $Hash
199225 } - Force
200226 } else {
201227 " Policy $ ( $Template.DisplayName ) not found in existing templates, creating new template"
@@ -212,17 +238,23 @@ function New-CIPPTemplateRun {
212238 JSON = " $object "
213239 RowKey = " $GUID "
214240 PartitionKey = ' IntuneTemplate'
241+ SHA = $Hash
215242 } - Force
216243 }
217-
218244 }
219245 }
220246
221247 ' intuneprotection' {
222248 Write-Information " Backup Intune Protection Policies for $TenantFilter "
223249 New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/deviceAppManagement/managedAppPolicies?$top=999' - tenantid $TenantFilter | ForEach-Object {
250+ $Hash = Get-StringHash - String (ConvertTo-Json - Depth 100 - Compress - InputObject $_ )
251+ $ExistingPolicy = $ExistingTemplates | Where-Object { $_.displayName -eq $_.DisplayName } | Select-Object - First 1
252+ if ($ExistingPolicy -and $ExistingPolicy.SHA -eq $Hash ) {
253+ " Policy $ ( $_.DisplayName ) found, SHA matches, skipping template creation"
254+ continue
255+ }
256+
224257 $Template = New-CIPPIntuneTemplate - TenantFilter $TenantFilter - URLName ' managedAppPolicies' - ID $_.ID
225- $ExistingPolicy = $ExistingTemplates | Where-Object { $_.displayName -eq $Template.DisplayName } | Select-Object - First 1
226258 if ($ExistingPolicy -and $ExistingPolicy.PartitionKey -eq ' IntuneTemplate' ) {
227259 " Policy $ ( $Template.DisplayName ) found, updating template"
228260 $object = [PSCustomObject ]@ {
@@ -237,6 +269,8 @@ function New-CIPPTemplateRun {
237269 JSON = " $object "
238270 RowKey = $ExistingPolicy.GUID
239271 PartitionKey = ' IntuneTemplate'
272+ Package = $ExistingPolicy.Package
273+ SHA = $Hash
240274 } - Force
241275 } else {
242276 " Policy $ ( $Template.DisplayName ) not found in existing templates, creating new template"
@@ -253,6 +287,7 @@ function New-CIPPTemplateRun {
253287 JSON = " $object "
254288 RowKey = " $GUID "
255289 PartitionKey = ' IntuneTemplate'
290+ SHA = $Hash
256291 } - Force
257292 }
258293 }
0 commit comments