-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/KelvinTegelaar/CIPP-API into…
… dev
- Loading branch information
Showing
3 changed files
with
155 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} | ||
} |