Skip to content

Commit

Permalink
group imports
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinTegelaar committed Feb 12, 2025
1 parent 0cce88a commit f2e3478
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
11 changes: 8 additions & 3 deletions Modules/CIPPCore/Public/New-CIPPTemplateRun.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function New-CIPPTemplateRun {
$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 | Add-Member -NotePropertyName 'SHA' -NotePropertyValue $_.SHA -Force
$data
} | Sort-Object -Property displayName

Expand All @@ -20,21 +21,25 @@ function New-CIPPTemplateRun {
}
if ($TemplateSettings.templateRepo) {
Write-Host 'Grabbing data from required community repo'
$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$', '' } }
$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$', '' } }
foreach ($File in $Files) {
$ExistingTemplate = $ExistingTemplates | Where-Object { $_.displayName -eq $File.name } | Select-Object -First 1
$Template = (Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepoBranch.value -Path $File.path).content | ConvertFrom-Json
if ($ExistingTemplate) {
$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
Write-Host "Template $($File.name) needs to be updated as the SHA is different"
Import-CommunityTemplate -Template $Template -SHA $File.sha
}
} else {
Write-Host "Template $($File.name) needs to be created"
Import-CommunityTemplate -Template $Template -SHA $File.sha

}
}

} else {
foreach ($Task in $Tasks) {
Write-Host "Working on task $Task"
Expand Down
23 changes: 22 additions & 1 deletion Modules/CIPPCore/Public/Tools/Import-CommunityTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,28 @@ function Import-CommunityTemplate {
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') {
if ($Template.groupTypes) { $Type = 'Group' }
if ($Template.'@odata.type' -like '*conditionalAccessPolicy*') { $Type = 'ConditionalAccessPolicy' }

switch -Wildcard ($Type) {
'*Group*' {
$RawJsonObj = [PSCustomObject]@{
Displayname = $Template.displayName
Description = $Template.Description
MembershipRules = $Template.membershipRule
username = $Template.mailNickname
GUID = $Template.id
groupType = 'generic'
} | ConvertTo-Json -Depth 100 -Compress
$entity = @{
JSON = "$RawJsonObj"
PartitionKey = 'GroupTemplate'
SHA = $SHA
GUID = $Template.id
RowKey = $Template.id
}
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
}
'*conditionalAccessPolicy*' {
$Template = ([pscustomobject]$Template) | ForEach-Object {
$NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object -ExpandProperty Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ function Get-GitHubFileContents {
$Path = "repos/$($FullName)/contents/$($Path)?ref=$($Branch)"
#Write-Information $Path
$File = Invoke-GitHubApiRequest -Path $Path -Method GET

$content = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($File.content))
#If the first character is a BOM, remove it
if ($content[0] -eq [char]65279) { $content = $content.Substring(1) }
return [PSCustomObject]@{
name = $File.name
path = $File.path
content = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($File.content))
content = $content
sha = $File.sha
size = $File.size
}
Expand Down

0 comments on commit f2e3478

Please sign in to comment.