@@ -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}
0 commit comments