Skip to content

Commit 634c0bc

Browse files
CA and groups
1 parent c64a5e9 commit 634c0bc

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

Modules/CIPPCore/Public/New-CIPPTemplateRun.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ function New-CIPPTemplateRun {
2222
if ($TemplateSettings.templateRepo) {
2323
Write-Host 'Grabbing data from required community repo'
2424
$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$', '' } }
25+
#if there is a migration table file, file the file. Store the file contents in $migrationtable
26+
$MigrationTable = $Files | Where-Object { $_.name -eq 'MigrationTable' } | Select-Object -Last 1
27+
if ($MigrationTable) {
28+
$MigrationTable = (Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepoBranch.value -Path $MigrationTable.path).content | ConvertFrom-Json
29+
}
2530
foreach ($File in $Files) {
31+
if ($File.name -eq 'MigrationTable') { continue }
2632
$ExistingTemplate = $ExistingTemplates | Where-Object { $_.displayName -eq $File.name } | Select-Object -First 1
2733
$Template = (Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepoBranch.value -Path $File.path).content | ConvertFrom-Json
2834
if ($ExistingTemplate) {
@@ -32,11 +38,11 @@ function New-CIPPTemplateRun {
3238
}
3339
if ($UpdateNeeded) {
3440
Write-Host "Template $($File.name) needs to be updated as the SHA is different"
35-
Import-CommunityTemplate -Template $Template -SHA $File.sha
41+
Import-CommunityTemplate -Template $Template -SHA $File.sha -MigrationTable $MigrationTable
3642
}
3743
} else {
3844
Write-Host "Template $($File.name) needs to be created"
39-
Import-CommunityTemplate -Template $Template -SHA $File.sha
45+
Import-CommunityTemplate -Template $Template -SHA $File.sha -MigrationTable $MigrationTable
4046

4147
}
4248
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function Import-CommunityTemplate {
77
[Parameter(Mandatory = $true)]
88
$Template,
99
$SHA,
10+
$MigrationTable,
1011
[switch]$Force
1112
)
1213

@@ -15,12 +16,14 @@ function Import-CommunityTemplate {
1516

1617
if ($Template.RowKey) {
1718
Write-Host "This is going to be a direct write to table, it's a CIPP template. We're writing $($Template.RowKey)"
19+
$Template = $Template | Select-Object * -ExcludeProperty timestamp
1820
Add-CIPPAzDataTableEntity @Table -Entity $Template -Force
1921
} else {
20-
if ($Template.groupTypes) { $Type = 'Group' }
22+
if ($Template.mailNickname) { $Type = 'Group' }
2123
if ($Template.'@odata.type' -like '*conditionalAccessPolicy*') { $Type = 'ConditionalAccessPolicy' }
22-
24+
Write-Host "The type is $Type"
2325
switch -Wildcard ($Type) {
26+
2427
'*Group*' {
2528
$RawJsonObj = [PSCustomObject]@{
2629
Displayname = $Template.displayName
@@ -29,7 +32,7 @@ function Import-CommunityTemplate {
2932
username = $Template.mailNickname
3033
GUID = $Template.id
3134
groupType = 'generic'
32-
} | ConvertTo-Json -Depth 100 -Compress
35+
} | ConvertTo-Json -Depth 100
3336
$entity = @{
3437
JSON = "$RawJsonObj"
3538
PartitionKey = 'GroupTemplate'
@@ -38,8 +41,10 @@ function Import-CommunityTemplate {
3841
RowKey = $Template.id
3942
}
4043
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
44+
break
4145
}
4246
'*conditionalAccessPolicy*' {
47+
Write-Host $MigrationTable
4348
$Template = ([pscustomobject]$Template) | ForEach-Object {
4449
$NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object -ExpandProperty Name
4550
$_ | Select-Object -Property $NonEmptyProperties
@@ -48,6 +53,12 @@ function Import-CommunityTemplate {
4853
$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'
4954
Remove-ODataProperties -Object $Template
5055
$RawJson = ConvertTo-Json -InputObject $Template -Depth 100 -Compress
56+
#Replace the ids with the displayname by using the migration table, this is a simple find and replace each instance in the JSON.
57+
$MigrationTable.objects | ForEach-Object {
58+
if ($RawJson -match $_.ID) {
59+
$RawJson = $RawJson.Replace($_.ID, $($_.DisplayName))
60+
}
61+
}
5162
$entity = @{
5263
JSON = "$RawJson"
5364
PartitionKey = 'CATemplate'
@@ -56,6 +67,7 @@ function Import-CommunityTemplate {
5667
RowKey = $ID
5768
}
5869
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
70+
break
5971
}
6072
default {
6173
$URLName = switch -Wildcard ($Template.'@odata.id') {

0 commit comments

Comments
 (0)