@@ -13,14 +13,21 @@ function New-CIPPTemplateRun {
1313 $data
1414 } | Sort-Object - Property displayName
1515
16+ function Get-SanitizedFilename {
17+ param (
18+ [string ]$filename
19+ )
20+ $filename = $filename -replace ' \s' , ' _' -replace ' [^\w\d_]' , ' '
21+ return $filename
22+ }
1623
1724 $Tasks = foreach ($key in $TemplateSettings.Keys ) {
1825 if ($TemplateSettings [$key ] -eq $true ) {
1926 $key
2027 }
2128 }
2229 if ($TemplateSettings.templateRepo ) {
23- Write-Host ' Grabbing data from required community repo'
30+ Write-Information ' Grabbing data from community repo'
2431 try {
2532 $Files = (Get-GitHubFileTree - FullName $TemplateSettings.templateRepo.value - Branch $TemplateSettings.templateRepoBranch.value ).tree | Where-Object { $_.path -match ' .json$' -and $_.path -notmatch ' NativeImport' } | Select-Object * , @ {n = ' html_url' ; e = { " https://github.com/$ ( $SplatParams.FullName ) /tree/$ ( $SplatParams.Branch ) /$ ( $_.path ) " } }, @ {n = ' name' ; e = { ($_.path -split ' /' )[ -1 ] -replace ' \.json$' , ' ' } }
2633 # if there is a migration table file, file the file. Store the file contents in $migrationtable
@@ -30,20 +37,29 @@ function New-CIPPTemplateRun {
3037 }
3138 foreach ($File in $Files ) {
3239 if ($File.name -eq ' MigrationTable' -or $file.name -eq ' ALLOWED COUNTRIES' ) { continue }
33- $ExistingTemplate = $ExistingTemplates | Where-Object { $_.displayName -eq $File.name } | Select-Object - First 1
34- $Template = (Get-GitHubFileContents - FullName $TemplateSettings.templateRepo.value - Branch $TemplateSettings.templateRepoBranch.value - Path $File.path ).content | ConvertFrom-Json
35- if ($ExistingTemplate ) {
36- $UpdateNeeded = $false
37- if ($ExistingTemplate.sha -ne $File.sha -or ! $ExistingTemplate.sha ) {
38- $UpdateNeeded = $true
39- }
40- if ($UpdateNeeded ) {
41- Write-Host " Template $ ( $File.name ) needs to be updated as the SHA is different"
42- Import-CommunityTemplate - Template $Template - SHA $File.sha - MigrationTable $MigrationTable
43- }
40+ $ExistingTemplate = $ExistingTemplates | Where-Object { (! [string ]::IsNullOrEmpty($_.displayName ) -and (Get-SanitizedFilename - filename $_.displayName ) -eq $File.name ) -or (! [string ]::IsNullOrEmpty($_.templateName ) -and (Get-SanitizedFilename - filename $_.templateName ) -eq $File.name ) } | Select-Object - First 1
41+
42+ $UpdateNeeded = $false
43+ if ($ExistingTemplate -and $ExistingTemplate.SHA -ne $File.sha ) {
44+ $Name = $ExistingTemplate.displayName ?? $ExistingTemplate.templateName
45+ Write-Information " Existing template $ ( $Name ) found, but SHA is different. Updating template."
46+ $UpdateNeeded = $true
47+ " Template $ ( $Name ) needs to be updated as the SHA is different"
4448 } else {
45- Write-Host " Template $ ( $File.name ) needs to be created"
49+ Write-Information " Existing template $ ( $File.name ) found, but SHA is the same. No update needed."
50+ " Template $ ( $File.name ) found, but SHA is the same. No update needed."
51+ }
52+
53+ if (! $ExistingTemplate -or $UpdateNeeded ) {
54+ $Template = (Get-GitHubFileContents - FullName $TemplateSettings.templateRepo.value - Branch $TemplateSettings.templateRepoBranch.value - Path $File.path ).content | ConvertFrom-Json
4655 Import-CommunityTemplate - Template $Template - SHA $File.sha - MigrationTable $MigrationTable
56+ if ($UpdateNeeded ) {
57+ Write-Information " Template $ ( $File.name ) needs to be updated as the SHA is different"
58+ " Template $ ( $File.name ) updated"
59+ } else {
60+ Write-Information " Template $ ( $File.name ) needs to be created"
61+ " Template $ ( $File.name ) created"
62+ }
4763 }
4864 }
4965 } catch {
@@ -53,12 +69,12 @@ function New-CIPPTemplateRun {
5369 }
5470 } else {
5571 foreach ($Task in $Tasks ) {
56- Write-Host " Working on task $Task "
72+ Write-Information " Working on task $Task "
5773 switch ($Task ) {
5874 ' ca' {
59- Write-Host " Template Conditional Access Policies for $TenantFilter "
75+ Write-Information " Template Conditional Access Policies for $TenantFilter "
6076 $Policies = New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/conditionalAccess/policies?$top=999' - tenantid $TenantFilter
61- Write-Host ' Creating templates for found Conditional Access Policies'
77+ Write-Information ' Creating templates for found Conditional Access Policies'
6278 foreach ($policy in $policies ) {
6379 try {
6480 $Template = New-CIPPCATemplate - TenantFilter $TenantFilter - JSON $policy
@@ -89,7 +105,7 @@ function New-CIPPTemplateRun {
89105 }
90106 }
91107 ' intuneconfig' {
92- Write-Host " Backup Intune Configuration Policies for $TenantFilter "
108+ Write-Information " Backup Intune Configuration Policies for $TenantFilter "
93109 $GraphURLS = @ (" https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations?`$ select=id,displayName,lastModifiedDateTime,roleScopeTagIds,microsoft.graph.unsupportedDeviceConfiguration/originalEntityTypeName&`$ expand=assignments&top=1000"
94110 ' https://graph.microsoft.com/beta/deviceManagement/windowsDriverUpdateProfiles'
95111 " https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations?`$ expand=assignments&top=999"
@@ -146,12 +162,12 @@ function New-CIPPTemplateRun {
146162 }
147163 }
148164 } catch {
149- Write-Host " Failed to backup $url "
165+ Write-Information " Failed to backup $url "
150166 }
151167 }
152168 }
153169 ' intunecompliance' {
154- Write-Host " Backup Intune Compliance Policies for $TenantFilter "
170+ Write-Information " Backup Intune Compliance Policies for $TenantFilter "
155171 New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies?$top=999' - tenantid $TenantFilter | ForEach-Object {
156172 $Template = New-CIPPIntuneTemplate - TenantFilter $TenantFilter - URLName ' deviceCompliancePolicies' - ID $_.ID
157173 $ExistingPolicy = $ExistingTemplates | Where-Object { $_.displayName -eq $Template.DisplayName } | Select-Object - First 1
@@ -192,7 +208,7 @@ function New-CIPPTemplateRun {
192208 }
193209
194210 ' intuneprotection' {
195- Write-Host " Backup Intune Protection Policies for $TenantFilter "
211+ Write-Information " Backup Intune Protection Policies for $TenantFilter "
196212 New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/deviceAppManagement/managedAppPolicies?$top=999' - tenantid $TenantFilter | ForEach-Object {
197213 $Template = New-CIPPIntuneTemplate - TenantFilter $TenantFilter - URLName ' managedAppPolicies' - ID $_.ID
198214 $ExistingPolicy = $ExistingTemplates | Where-Object { $_.displayName -eq $Template.DisplayName } | Select-Object - First 1
0 commit comments