Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/KelvinTegelaar/CIPP-API into…
Browse files Browse the repository at this point in the history
… dev
  • Loading branch information
JohnDuprey committed Feb 12, 2025
2 parents d0a85ed + 2611236 commit f90a8c2
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 24 deletions.
19 changes: 3 additions & 16 deletions Modules/CIPPCore/Public/New-CIPPTemplateRun.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function New-CIPPTemplateRun {
)
$Table = Get-CippTable -tablename 'templates'
$ExistingTemplates = (Get-CIPPAzDataTableEntity @Table) | ForEach-Object {
$data = $_.JSON | ConvertFrom-Json -Depth 100
$data = $_.JSON | ConvertFrom-Json -ErrorAction SilentlyContinue -Depth 100
$data | Add-Member -NotePropertyName 'GUID' -NotePropertyValue $_.RowKey -Force
$data | Add-Member -NotePropertyName 'PartitionKey' -NotePropertyValue $_.PartitionKey -Force
$data
Expand All @@ -20,30 +20,17 @@ function New-CIPPTemplateRun {
}
if ($TemplateSettings.templateRepo) {
Write-Host 'Grabbing data from required community repo'
<#$RepoURI = "https://geoipdb.azurewebsites.net/api/GetTemplateRepo?repo=$($TemplateSettings.templateRepo.value)"
$RepoData = Invoke-RestMethod -Uri $RepoURI -Method GET -ContentType 'application/json'
$ImportTemplates = foreach ($task in $Tasks) {
switch ($Task) {
'caTemplates' { $RepoData.ca }
'policyTemplates' { $RepoData.policyTemplates }
'groupTemplates' { $RepoData.groupTemplates }
'standardTemplates' { $RepoData.standardTemplates }
}
}#>
$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$', '' } }

foreach ($File in $Files) {
# find file.name in existing templates
$ExistingTemplate = $ExistingTemplates | Where-Object { $_.displayName -eq $File.name } | Select-Object -First 1
if ($ExistingTemplate) {
# check the sha hash of the file against the existing template
$UpdateNeeded = $false
if ($ExistingTemplate.sha -ne $File.sha -or !$ExistingTemplate.sha) {
$UpdateNeeded = $true
}

if ($UpdateNeeded) {

$Template = Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepo.branch -Path $File.path | ConvertFrom-Json
Import-CommunityTemplate -Template $Template -SHA $File.sha
}
}
}
Expand Down
66 changes: 58 additions & 8 deletions Modules/CIPPCore/Public/Tools/Import-CommunityTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,69 @@ function Import-CommunityTemplate {
param(
[Parameter(Mandatory = $true)]
$Template,
$SHA,
[switch]$Force
)

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

$CippTemplates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) | ForEach-Object {
$GUID = $_.RowKey
$data = $_.JSON | ConvertFrom-Json
$data | Add-Member -NotePropertyName 'GUID' -NotePropertyValue $GUID -Force
$data

if ($Template.RowKey) {
Write-Host "This is going to be a direct write to table, it's a CIPP template. We're writing $($Template.RowKey)"
Add-CIPPAzDataTableEntity @Table -Entity $Template -Force
} else {
switch -Wildcard ($Template.'@odata.type') {
'*conditionalAccessPolicy*' {
$Template = ([pscustomobject]$Template) | ForEach-Object {
$NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object -ExpandProperty Name
$_ | Select-Object -Property $NonEmptyProperties
}
$id = $Template.id
$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]'
Remove-ODataProperties -Object $Template
$RawJson = ConvertTo-Json -InputObject $Template -Depth 100 -Compress
$entity = @{
JSON = "$RawJson"
PartitionKey = 'CATemplate'
SHA = $SHA
GUID = $ID
RowKey = $ID
}
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
}
default {
$URLName = switch -Wildcard ($Template.'@odata.id') {
'*CompliancePolicies*' { 'DeviceCompliancePolicies' }
'*deviceConfigurations*' { 'Device' }
'*DriverUpdateProfiles*' { 'windowsDriverUpdateProfiles' }
'*SettingsCatalog*' { 'Catalog' }
'*configurationPolicies*' { 'Catalog' }
}
$id = $Template.id
$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]'
Remove-ODataProperties -Object $RawJson
$RawJson = $RawJson | ConvertTo-Json -Depth 100 -Compress

#create a new template
$RawJsonObj = [PSCustomObject]@{
Displayname = $Template.displayName ?? $template.Name
Description = $Template.Description
RAWJson = $RawJson
Type = $URLName
GUID = $ID
} | ConvertTo-Json -Depth 100 -Compress

$entity = @{
JSON = "$RawJsonObj"
PartitionKey = 'IntuneTemplate'
SHA = $SHA
GUID = $ID
RowKey = $ID
}
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force

}
}
}

$Contents = $Template.content
Write-Host ($Contents)
}
94 changes: 94 additions & 0 deletions Modules/CIPPCore/Public/Tools/Remove-ODataProperties.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
function Remove-ODataProperties {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
$Object,
[switch]$SkipRemovingProperties,
[string[]]$PropertiesToRemove = @(),
[string[]]$SkipRemoveProperties = @(),
[switch]$SkipRemoveDefaultProperties,
[switch]$SkipRemovingChildProperties
)
if ($SkipRemovingProperties) {
return
}
$defaultProperties = @(
'id',
'createdDateTime',
'lastModifiedDateTime',
'supportsScopeTags',
'modifiedDateTime'
)
if (-not $Object) {
return
}
$removeProps = New-Object System.Collections.Generic.List[string]
if ($PropertiesToRemove) {
$removeProps.AddRange($PropertiesToRemove)
}
if (-not $SkipRemoveDefaultProperties) {
foreach ($defProp in $defaultProperties) {
if (-not $removeProps.Contains($defProp)) {
$removeProps.Add($defProp)
}
}
}
function Remove-PropertyIfPresent {
param(
[Parameter(Mandatory)]
$psObject,
[Parameter(Mandatory)]
[string]$propName
)
$propExists = $psObject.PSObject.Properties | Where-Object { $_.Name -eq $propName }
if ($propExists) {
$psObject.PSObject.Properties.Remove($propName) | Out-Null
}
}

if ($Object -is [System.Collections.IEnumerable] -and -not ($Object -is [string])) {
foreach ($element in $Object) {
Remove-ODataProperties -Object $element -SkipRemovingProperties:$SkipRemovingProperties -PropertiesToRemove $PropertiesToRemove -SkipRemoveProperties $SkipRemoveProperties -SkipRemoveDefaultProperties:$SkipRemoveDefaultProperties -SkipRemovingChildProperties:$SkipRemovingChildProperties
}
return
}
if ($Object -is [PSCustomObject]) {
$odataProps = $Object.PSObject.Properties | Where-Object {
$_.Name -like '*@odata*Link' -or
$_.Name -like '*@odata.context' -or
$_.Name -like '*@odata.id' -or
($_.Name -like '*@odata.type' -and $_.Name -ne '@odata.type')
}

foreach ($oProp in $odataProps) {
if (-not $removeProps.Contains($oProp.Name)) {
$removeProps.Add($oProp.Name)
}
}

foreach ($propName in $removeProps) {
if ($SkipRemoveProperties -notcontains $propName) {
Remove-PropertyIfPresent -psObject $Object -propName $propName
}
}

if (-not $SkipRemovingChildProperties) {
foreach ($prop in $Object.PSObject.Properties) {
$val = $prop.Value

if ($val -is [System.Collections.IEnumerable] -and -not ($val -is [string])) {
foreach ($child in $val) {

if ($child -is [PSCustomObject]) {
Remove-ODataProperties -Object $child -SkipRemovingProperties:$SkipRemovingProperties -PropertiesToRemove $PropertiesToRemove -SkipRemoveProperties $SkipRemoveProperties -SkipRemoveDefaultProperties:$SkipRemoveDefaultProperties -SkipRemovingChildProperties:$SkipRemovingChildProperties
}
}
}
# If $val is a single PSCustomObject, recurse into it as well.
elseif ($val -is [PSCustomObject]) {
Remove-ODataProperties -Object $val -SkipRemovingProperties:$SkipRemovingProperties -PropertiesToRemove $PropertiesToRemove -SkipRemoveProperties $SkipRemoveProperties -SkipRemoveDefaultProperties:$SkipRemoveDefaultProperties -SkipRemovingChildProperties:$SkipRemovingChildProperties
}
}
}
}
}

0 comments on commit f90a8c2

Please sign in to comment.