Skip to content

Commit e1a26bc

Browse files
committed
custom data sync tweaks
1 parent 9267652 commit e1a26bc

File tree

2 files changed

+135
-11
lines changed

2 files changed

+135
-11
lines changed

Modules/CIPPCore/Public/CustomData/Invoke-CustomDataSync.ps1

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,134 @@ function Invoke-CustomDataSync {
33
$TenantFilter
44
)
55

6-
$Table = Get-CIPPTable -TableName CustomDataMapping
6+
$Table = Get-CIPPTable -TableName CustomDataMappings
77
$CustomData = Get-CIPPAzDataTableEntity @Table
88

99
$Mappings = $CustomData | ForEach-Object {
1010
$Mapping = $_.JSON | ConvertFrom-Json
1111
$Mapping
1212
}
1313

14-
Write-Host ($Mappings | ConvertTo-Json -Depth 10)
14+
Write-Information "Found $($Mappings.Count) Custom Data mappings"
15+
$Mappings = $Mappings | Where-Object { $_.sourceType.value -eq 'extensionSync' -and $_.tenantFilter.value -contains $TenantFilter -or $_.tenantFilter.value -contains 'AllTenants' }
16+
17+
if ($Mappings.Count -eq 0) {
18+
Write-Warning "No Custom Data mappings found for tenant $TenantFilter"
19+
return
20+
}
21+
22+
Write-Information "Getting cached data for tenant $TenantFilter"
23+
$Cache = Get-ExtensionCacheData -TenantFilter $TenantFilter
24+
$BulkRequests = [System.Collections.Generic.List[object]]::new()
25+
$DirectoryObjectQueries = [System.Collections.Generic.List[object]]::new()
26+
$SyncConfigs = foreach ($Mapping in $Mappings) {
27+
$SyncConfig = [PSCustomObject]@{
28+
Dataset = $Mapping.extensionSyncDataset.value
29+
DatasetConfig = $Mapping.extensionSyncDataset.addedFields
30+
DirectoryObjectType = $Mapping.directoryObjectType.value
31+
ExtensionSyncProperty = $Mapping.extensionSyncProperty.value
32+
CustomDataAttribute = $Mapping.customDataAttribute.value
33+
CustomDataAttributeConfig = $Mapping.customDataAttribute.addedFields
34+
}
35+
36+
switch ($SyncConfig.DirectoryObjectType) {
37+
'user' {
38+
$Query = @{
39+
id = 'user'
40+
url = 'users?$select=id,userPrincipalName,displayName&$count=true&$top=999'
41+
method = 'GET'
42+
}
43+
}
44+
}
45+
$DirectoryObjectQueries.Add($Query)
46+
$SyncConfig
47+
}
48+
49+
Write-Information "Getting directory objects for tenant $TenantFilter"
50+
#Write-Information ($DirectoryObjectQueries | ConvertTo-Json -Depth 10)
51+
$AllDirectoryObjects = New-GraphBulkRequest -tenantid $TenantFilter -Requests @($DirectoryObjectQueries)
52+
53+
foreach ($SyncConfig in $SyncConfigs) {
54+
Write-Warning "Processing Custom Data mapping for $($Mapping.customDataAttribute.value)"
55+
Write-Information ($SyncConfig | ConvertTo-Json -Depth 10)
56+
$Rows = $Cache.$($SyncConfig.Dataset)
57+
if (!$Rows) {
58+
Write-Warning "No data found for dataset $($SyncConfig.Dataset)"
59+
continue
60+
}
61+
$SourceMatchProperty = $SyncConfig.DatasetConfig.sourceMatchProperty
62+
$DestinationMatchProperty = $SyncConfigs.DatasetConfig.destinationMatchProperty
63+
$CustomDataAttribute = $SyncConfig.CustomDataAttribute
64+
$ExtensionSyncProperty = $SyncConfig.ExtensionSyncProperty
65+
$DatasetConfig = $SyncConfig.DatasetConfig
66+
67+
$DirectoryObjects = ($AllDirectoryObjects | Where-Object { $_.id -eq $SyncConfig.DirectoryObjectType }).body.value
68+
69+
switch ($SyncConfig.DirectoryObjectType) {
70+
'user' {
71+
$url = 'users'
72+
}
73+
}
74+
75+
foreach ($Row in $Rows) {
76+
#Write-Warning 'Processing row'
77+
#Write-Information ($Row | ConvertTo-Json -Depth 10)
78+
#Write-Host "Comparing $SourceMatchProperty $($Row.$SourceMatchProperty) to $($DirectoryObjects.Count) directory objects on $DestinationMatchProperty"
79+
$DirectoryObject = $DirectoryObjects | Where-Object { $_.$DestinationMatchProperty -eq $Row.$SourceMatchProperty }
80+
if (!$DirectoryObject) {
81+
Write-Warning "No directory object found for $($Row.$SourceMatchProperty)"
82+
}
83+
if ($DirectoryObject) {
84+
$ObjectUrl = "$($url)/$($DirectoryObject.id)"
85+
86+
if ($DatasetConfig.type -eq 'object') {
87+
if ($CustomDataAttribute -match '\.') {
88+
$Props = @($CustomDataAttribute -split '\.')
89+
$Body = [PSCustomObject]@{
90+
$Props[0] = [PSCustomObject]@{
91+
$Props[1] = $Row.$ExtensionSyncProperty
92+
}
93+
}
94+
} else {
95+
$Body = [PSCustomObject]@{
96+
$CustomDataAttribute = @($Row.$ExtensionSyncProperty)
97+
}
98+
}
99+
} elseif ($DatasetConfig.type -eq 'array') {
100+
101+
$Data = foreach ($Entry in $Row.$ExtensionSyncProperty) {
102+
if ($DatasetConfig.storeAs -eq 'json') {
103+
$Entry | ConvertTo-Json -Depth 5 -Compress
104+
} else {
105+
$Entry
106+
}
107+
}
108+
109+
$Body = [PSCustomObject]@{
110+
$CustomDataAttribute = @($Data)
111+
}
112+
}
113+
114+
$BulkRequests.Add([PSCustomObject]@{
115+
id = $DirectoryObject.$DestinationMatchProperty
116+
url = $ObjectUrl
117+
method = 'PATCH'
118+
headers = @{
119+
'Content-Type' = 'application/json'
120+
}
121+
body = $Body.PSObject.Copy()
122+
})
123+
}
124+
}
125+
}
126+
127+
#Write-Host ($BulkRequests | ConvertTo-Json -Depth 10)
128+
if ($BulkRequests.Count -gt 0) {
129+
Write-Information "Sending $($BulkRequests.Count) requests to Graph API"
130+
$Responses = New-GraphBulkRequest -tenantid $TenantFilter -Requests @($BulkRequests)
131+
if ($Responses | Where-Object { $_.statusCode -ne 204 }) {
132+
Write-Warning 'Some requests failed'
133+
Write-Information ($Responses | Where-Object { $_.statusCode -ne 204 } | Select-Object -Property id, statusCode | ConvertTo-Json)
134+
}
135+
}
15136
}

Modules/CippExtensions/Public/Extension Functions/Register-CippExtensionScheduledTasks.ps1

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ function Register-CIPPExtensionScheduledTasks {
2323
$CustomDataMappingTable = Get-CIPPTable -TableName CustomDataMappings
2424
$Mappings = Get-CIPPAzDataTableEntity @CustomDataMappingTable | ForEach-Object {
2525
$Mapping = $_.JSON | ConvertFrom-Json
26-
27-
$TenantMappings = if ($Mapping.tenantFilter.value -eq 'AllTenants') {
28-
$Tenants
29-
} else {
30-
$Tenants | Where-Object { $_.customerId -eq $Mapping.tenantFilter.value -or $_.defaultDomainName -eq $Mapping.tenantFilter.value }
31-
}
32-
foreach ($TenantMapping in $TenantMappings) {
33-
[pscustomobject]@{
34-
RowKey = $TenantMapping.customerId
26+
if ($Mapping.sourceType -eq 'extensionSync') {
27+
$TenantMappings = if ($Mapping.tenantFilter.value -contains 'AllTenants') {
28+
$Tenants
29+
} else {
30+
foreach ($TenantMapping in $TenantMappings) {
31+
$TenantMapping | Where-Object { $_.customerId -eq $Mapping.tenantFilter.value -or $_.defaultDomainName -eq $Mapping.tenantFilter.value }
32+
}
33+
}
34+
foreach ($TenantMapping in $TenantMappings) {
35+
[pscustomobject]@{
36+
RowKey = $TenantMapping.customerId
37+
}
3538
}
3639
}
3740
} | Sort-Object -Property RowKey -Unique

0 commit comments

Comments
 (0)