Skip to content

Commit 2611236

Browse files
template changes
1 parent a743f7f commit 2611236

3 files changed

Lines changed: 155 additions & 24 deletions

File tree

Modules/CIPPCore/Public/New-CIPPTemplateRun.ps1

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function New-CIPPTemplateRun {
66
)
77
$Table = Get-CippTable -tablename 'templates'
88
$ExistingTemplates = (Get-CIPPAzDataTableEntity @Table) | ForEach-Object {
9-
$data = $_.JSON | ConvertFrom-Json -Depth 100
9+
$data = $_.JSON | ConvertFrom-Json -ErrorAction SilentlyContinue -Depth 100
1010
$data | Add-Member -NotePropertyName 'GUID' -NotePropertyValue $_.RowKey -Force
1111
$data | Add-Member -NotePropertyName 'PartitionKey' -NotePropertyValue $_.PartitionKey -Force
1212
$data
@@ -20,30 +20,17 @@ function New-CIPPTemplateRun {
2020
}
2121
if ($TemplateSettings.templateRepo) {
2222
Write-Host 'Grabbing data from required community repo'
23-
<#$RepoURI = "https://geoipdb.azurewebsites.net/api/GetTemplateRepo?repo=$($TemplateSettings.templateRepo.value)"
24-
$RepoData = Invoke-RestMethod -Uri $RepoURI -Method GET -ContentType 'application/json'
25-
$ImportTemplates = foreach ($task in $Tasks) {
26-
switch ($Task) {
27-
'caTemplates' { $RepoData.ca }
28-
'policyTemplates' { $RepoData.policyTemplates }
29-
'groupTemplates' { $RepoData.groupTemplates }
30-
'standardTemplates' { $RepoData.standardTemplates }
31-
}
32-
}#>
3323
$Files = (Get-GitHubFileTree -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepo.branch).tree | Where-Object { $_.path -match '.json$' } | Select-Object *, @{n = 'html_url'; e = { "https://github.com/$($SplatParams.FullName)/tree/$($SplatParams.Branch)/$($_.path)" } }, @{n = 'name'; e = { ($_.path -split '/')[ -1 ] -replace '\.json$', '' } }
34-
3524
foreach ($File in $Files) {
36-
# find file.name in existing templates
3725
$ExistingTemplate = $ExistingTemplates | Where-Object { $_.displayName -eq $File.name } | Select-Object -First 1
3826
if ($ExistingTemplate) {
39-
# check the sha hash of the file against the existing template
4027
$UpdateNeeded = $false
4128
if ($ExistingTemplate.sha -ne $File.sha -or !$ExistingTemplate.sha) {
4229
$UpdateNeeded = $true
4330
}
44-
4531
if ($UpdateNeeded) {
46-
32+
$Template = Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepo.branch -Path $File.path | ConvertFrom-Json
33+
Import-CommunityTemplate -Template $Template -SHA $File.sha
4734
}
4835
}
4936
}

Modules/CIPPCore/Public/Tools/Import-CommunityTemplate.ps1

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,69 @@ function Import-CommunityTemplate {
66
param(
77
[Parameter(Mandatory = $true)]
88
$Template,
9+
$SHA,
910
[switch]$Force
1011
)
1112

1213
$Table = Get-CippTable -TableName 'templates'
13-
$Filter = "PartitionKey eq '$Type'"
1414

15-
$CippTemplates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) | ForEach-Object {
16-
$GUID = $_.RowKey
17-
$data = $_.JSON | ConvertFrom-Json
18-
$data | Add-Member -NotePropertyName 'GUID' -NotePropertyValue $GUID -Force
19-
$data
15+
16+
if ($Template.RowKey) {
17+
Write-Host "This is going to be a direct write to table, it's a CIPP template. We're writing $($Template.RowKey)"
18+
Add-CIPPAzDataTableEntity @Table -Entity $Template -Force
19+
} else {
20+
switch -Wildcard ($Template.'@odata.type') {
21+
'*conditionalAccessPolicy*' {
22+
$Template = ([pscustomobject]$Template) | ForEach-Object {
23+
$NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object -ExpandProperty Name
24+
$_ | Select-Object -Property $NonEmptyProperties
25+
}
26+
$id = $Template.id
27+
$Template = $Template | Select-Object * -ExcludeProperty lastModifiedDateTime, 'assignments', '#microsoft*', '*@odata.navigationLink', '*@odata.associationLink', '*@odata.context', 'ScopeTagIds', 'supportsScopeTags', 'createdDateTime', '@odata.id', '@odata.editLink', '*odata.type', 'roleScopeTagIds@odata.type', createdDateTime, 'createdDateTime@odata.type'
28+
Remove-ODataProperties -Object $Template
29+
$RawJson = ConvertTo-Json -InputObject $Template -Depth 100 -Compress
30+
$entity = @{
31+
JSON = "$RawJson"
32+
PartitionKey = 'CATemplate'
33+
SHA = $SHA
34+
GUID = $ID
35+
RowKey = $ID
36+
}
37+
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
38+
}
39+
default {
40+
$URLName = switch -Wildcard ($Template.'@odata.id') {
41+
'*CompliancePolicies*' { 'DeviceCompliancePolicies' }
42+
'*deviceConfigurations*' { 'Device' }
43+
'*DriverUpdateProfiles*' { 'windowsDriverUpdateProfiles' }
44+
'*SettingsCatalog*' { 'Catalog' }
45+
'*configurationPolicies*' { 'Catalog' }
46+
}
47+
$id = $Template.id
48+
$RawJson = $Template | Select-Object * -ExcludeProperty id, lastModifiedDateTime, 'assignments', '#microsoft*', '*@odata.navigationLink', '*@odata.associationLink', '*@odata.context', 'ScopeTagIds', 'supportsScopeTags', 'createdDateTime', '@odata.id', '@odata.editLink', 'lastModifiedDateTime@odata.type', 'roleScopeTagIds@odata.type', createdDateTime, 'createdDateTime@odata.type'
49+
Remove-ODataProperties -Object $RawJson
50+
$RawJson = $RawJson | ConvertTo-Json -Depth 100 -Compress
51+
52+
#create a new template
53+
$RawJsonObj = [PSCustomObject]@{
54+
Displayname = $Template.displayName ?? $template.Name
55+
Description = $Template.Description
56+
RAWJson = $RawJson
57+
Type = $URLName
58+
GUID = $ID
59+
} | ConvertTo-Json -Depth 100 -Compress
60+
61+
$entity = @{
62+
JSON = "$RawJsonObj"
63+
PartitionKey = 'IntuneTemplate'
64+
SHA = $SHA
65+
GUID = $ID
66+
RowKey = $ID
67+
}
68+
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
69+
70+
}
71+
}
2072
}
2173

22-
$Contents = $Template.content
23-
Write-Host ($Contents)
2474
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
function Remove-ODataProperties {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter(Mandatory)]
5+
$Object,
6+
[switch]$SkipRemovingProperties,
7+
[string[]]$PropertiesToRemove = @(),
8+
[string[]]$SkipRemoveProperties = @(),
9+
[switch]$SkipRemoveDefaultProperties,
10+
[switch]$SkipRemovingChildProperties
11+
)
12+
if ($SkipRemovingProperties) {
13+
return
14+
}
15+
$defaultProperties = @(
16+
'id',
17+
'createdDateTime',
18+
'lastModifiedDateTime',
19+
'supportsScopeTags',
20+
'modifiedDateTime'
21+
)
22+
if (-not $Object) {
23+
return
24+
}
25+
$removeProps = New-Object System.Collections.Generic.List[string]
26+
if ($PropertiesToRemove) {
27+
$removeProps.AddRange($PropertiesToRemove)
28+
}
29+
if (-not $SkipRemoveDefaultProperties) {
30+
foreach ($defProp in $defaultProperties) {
31+
if (-not $removeProps.Contains($defProp)) {
32+
$removeProps.Add($defProp)
33+
}
34+
}
35+
}
36+
function Remove-PropertyIfPresent {
37+
param(
38+
[Parameter(Mandatory)]
39+
$psObject,
40+
[Parameter(Mandatory)]
41+
[string]$propName
42+
)
43+
$propExists = $psObject.PSObject.Properties | Where-Object { $_.Name -eq $propName }
44+
if ($propExists) {
45+
$psObject.PSObject.Properties.Remove($propName) | Out-Null
46+
}
47+
}
48+
49+
if ($Object -is [System.Collections.IEnumerable] -and -not ($Object -is [string])) {
50+
foreach ($element in $Object) {
51+
Remove-ODataProperties -Object $element -SkipRemovingProperties:$SkipRemovingProperties -PropertiesToRemove $PropertiesToRemove -SkipRemoveProperties $SkipRemoveProperties -SkipRemoveDefaultProperties:$SkipRemoveDefaultProperties -SkipRemovingChildProperties:$SkipRemovingChildProperties
52+
}
53+
return
54+
}
55+
if ($Object -is [PSCustomObject]) {
56+
$odataProps = $Object.PSObject.Properties | Where-Object {
57+
$_.Name -like '*@odata*Link' -or
58+
$_.Name -like '*@odata.context' -or
59+
$_.Name -like '*@odata.id' -or
60+
($_.Name -like '*@odata.type' -and $_.Name -ne '@odata.type')
61+
}
62+
63+
foreach ($oProp in $odataProps) {
64+
if (-not $removeProps.Contains($oProp.Name)) {
65+
$removeProps.Add($oProp.Name)
66+
}
67+
}
68+
69+
foreach ($propName in $removeProps) {
70+
if ($SkipRemoveProperties -notcontains $propName) {
71+
Remove-PropertyIfPresent -psObject $Object -propName $propName
72+
}
73+
}
74+
75+
if (-not $SkipRemovingChildProperties) {
76+
foreach ($prop in $Object.PSObject.Properties) {
77+
$val = $prop.Value
78+
79+
if ($val -is [System.Collections.IEnumerable] -and -not ($val -is [string])) {
80+
foreach ($child in $val) {
81+
82+
if ($child -is [PSCustomObject]) {
83+
Remove-ODataProperties -Object $child -SkipRemovingProperties:$SkipRemovingProperties -PropertiesToRemove $PropertiesToRemove -SkipRemoveProperties $SkipRemoveProperties -SkipRemoveDefaultProperties:$SkipRemoveDefaultProperties -SkipRemovingChildProperties:$SkipRemovingChildProperties
84+
}
85+
}
86+
}
87+
# If $val is a single PSCustomObject, recurse into it as well.
88+
elseif ($val -is [PSCustomObject]) {
89+
Remove-ODataProperties -Object $val -SkipRemovingProperties:$SkipRemovingProperties -PropertiesToRemove $PropertiesToRemove -SkipRemoveProperties $SkipRemoveProperties -SkipRemoveDefaultProperties:$SkipRemoveDefaultProperties -SkipRemovingChildProperties:$SkipRemovingChildProperties
90+
}
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)