Skip to content

Commit f2e3478

Browse files
group imports
1 parent 0cce88a commit f2e3478

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

Modules/CIPPCore/Public/New-CIPPTemplateRun.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function New-CIPPTemplateRun {
99
$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
12+
$data | Add-Member -NotePropertyName 'SHA' -NotePropertyValue $_.SHA -Force
1213
$data
1314
} | Sort-Object -Property displayName
1415

@@ -20,21 +21,25 @@ function New-CIPPTemplateRun {
2021
}
2122
if ($TemplateSettings.templateRepo) {
2223
Write-Host 'Grabbing data from required community repo'
23-
$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$', '' } }
24+
$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$', '' } }
2425
foreach ($File in $Files) {
2526
$ExistingTemplate = $ExistingTemplates | Where-Object { $_.displayName -eq $File.name } | Select-Object -First 1
27+
$Template = (Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepoBranch.value -Path $File.path).content | ConvertFrom-Json
2628
if ($ExistingTemplate) {
2729
$UpdateNeeded = $false
2830
if ($ExistingTemplate.sha -ne $File.sha -or !$ExistingTemplate.sha) {
2931
$UpdateNeeded = $true
3032
}
3133
if ($UpdateNeeded) {
32-
$Template = Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepo.branch -Path $File.path | ConvertFrom-Json
34+
Write-Host "Template $($File.name) needs to be updated as the SHA is different"
3335
Import-CommunityTemplate -Template $Template -SHA $File.sha
3436
}
37+
} else {
38+
Write-Host "Template $($File.name) needs to be created"
39+
Import-CommunityTemplate -Template $Template -SHA $File.sha
40+
3541
}
3642
}
37-
3843
} else {
3944
foreach ($Task in $Tasks) {
4045
Write-Host "Working on task $Task"

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,28 @@ function Import-CommunityTemplate {
1717
Write-Host "This is going to be a direct write to table, it's a CIPP template. We're writing $($Template.RowKey)"
1818
Add-CIPPAzDataTableEntity @Table -Entity $Template -Force
1919
} else {
20-
switch -Wildcard ($Template.'@odata.type') {
20+
if ($Template.groupTypes) { $Type = 'Group' }
21+
if ($Template.'@odata.type' -like '*conditionalAccessPolicy*') { $Type = 'ConditionalAccessPolicy' }
22+
23+
switch -Wildcard ($Type) {
24+
'*Group*' {
25+
$RawJsonObj = [PSCustomObject]@{
26+
Displayname = $Template.displayName
27+
Description = $Template.Description
28+
MembershipRules = $Template.membershipRule
29+
username = $Template.mailNickname
30+
GUID = $Template.id
31+
groupType = 'generic'
32+
} | ConvertTo-Json -Depth 100 -Compress
33+
$entity = @{
34+
JSON = "$RawJsonObj"
35+
PartitionKey = 'GroupTemplate'
36+
SHA = $SHA
37+
GUID = $Template.id
38+
RowKey = $Template.id
39+
}
40+
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
41+
}
2142
'*conditionalAccessPolicy*' {
2243
$Template = ([pscustomobject]$Template) | ForEach-Object {
2344
$NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object -ExpandProperty Name

Modules/CippExtensions/Public/GitHub/Get-GitHubFileContents.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ function Get-GitHubFileContents {
1515
$Path = "repos/$($FullName)/contents/$($Path)?ref=$($Branch)"
1616
#Write-Information $Path
1717
$File = Invoke-GitHubApiRequest -Path $Path -Method GET
18-
18+
$content = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($File.content))
19+
#If the first character is a BOM, remove it
20+
if ($content[0] -eq [char]65279) { $content = $content.Substring(1) }
1921
return [PSCustomObject]@{
2022
name = $File.name
2123
path = $File.path
22-
content = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($File.content))
24+
content = $content
2325
sha = $File.sha
2426
size = $File.size
2527
}

0 commit comments

Comments
 (0)