@@ -37,21 +37,29 @@ function Invoke-CustomDataSync {
3737 ' user' {
3838 $Query = @ {
3939 id = ' user'
40- url = ' users?$select=id,userPrincipalName,displayName&$count=true&$top=999'
40+ url = ' users?$select=id,userPrincipalName,displayName,mailNickname &$count=true&$top=999'
4141 method = ' GET'
4242 }
4343 }
4444 }
45- $DirectoryObjectQueries.Add ($Query )
4645 $SyncConfig
46+ if ($DirectoryObjectQueries | Where-Object { $_.id -eq $Query.id }) {
47+ continue
48+ } else {
49+ $DirectoryObjectQueries.Add ($Query )
50+ }
4751 }
4852
4953 Write-Information " Getting directory objects for tenant $TenantFilter "
5054 # Write-Information ($DirectoryObjectQueries | ConvertTo-Json -Depth 10)
5155 $AllDirectoryObjects = New-GraphBulkRequest - tenantid $TenantFilter - Requests @ ($DirectoryObjectQueries )
56+ Write-Information " Retrieved $ ( $AllDirectoryObjects.Count ) result sets"
57+ # Write-Information ($AllDirectoryObjects | ConvertTo-Json -Depth 10)
58+
59+ $PatchObjects = @ {}
5260
5361 foreach ($SyncConfig in $SyncConfigs ) {
54- Write-Warning " Processing Custom Data mapping for $ ( $Mapping .customDataAttribute.value ) "
62+ Write-Warning " Processing Custom Data mapping for $ ( $SyncConfig .Dataset ) "
5563 Write-Information ($SyncConfig | ConvertTo-Json - Depth 10 )
5664 $Rows = $Cache .$ ($SyncConfig.Dataset )
5765 if (! $Rows ) {
@@ -75,62 +83,87 @@ function Invoke-CustomDataSync {
7583 foreach ($Row in $Rows ) {
7684 # Write-Warning 'Processing row'
7785 # 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 }
86+ Write-Host " Comparing $SourceMatchProperty $ ( $Row .$SourceMatchProperty ) to $ ( $DirectoryObjects.Count ) directory objects on $DestinationMatchProperty "
87+ if ($DestinationMatchProperty.Count -gt 1 ) {
88+ foreach ($Prop in $DestinationMatchProperty ) {
89+ $DirectoryObject = $DirectoryObjects | Where-Object { $_ .$Prop -eq $Row .$SourceMatchProperty }
90+ if ($DirectoryObject ) {
91+ break
92+ }
93+ }
94+ } else {
95+ $DirectoryObject = $DirectoryObjects | Where-Object { $_ .$DestinationMatchProperty -eq $Row .$SourceMatchProperty }
96+ }
97+
8098 if (! $DirectoryObject ) {
8199 Write-Warning " No directory object found for $ ( $Row .$SourceMatchProperty ) "
82100 }
83101 if ($DirectoryObject ) {
84102 $ObjectUrl = " $ ( $url ) /$ ( $DirectoryObject.id ) "
85103
104+ # check if key in patch objects already exists otherwise create one with object url
105+ if (! $PatchObjects.ContainsKey ($ObjectUrl )) {
106+ Write-Host " Creating new object for $ ( $ObjectUrl ) "
107+ $PatchObjects [$ObjectUrl ] = @ {}
108+ }
109+
86110 if ($DatasetConfig.type -eq ' object' ) {
87111 if ($CustomDataAttribute -match ' \.' ) {
88112 $Props = @ ($CustomDataAttribute -split ' \.' )
89- $Body = [PSCustomObject ]@ {
90- $Props [0 ] = [PSCustomObject ]@ {
91- $Props [1 ] = $Row .$ExtensionSyncProperty
92- }
113+ if (! $PatchObjects [$ObjectUrl ].ContainsKey($Props [0 ])) {
114+ Write-Host " Creating new object for $ ( $Props [0 ]) "
115+ $PatchObjects [$ObjectUrl ][$Props [0 ]] = @ {}
93116 }
94- } else {
95- $Body = [ PSCustomObject ] @ {
96- $CustomDataAttribute = @ ( $Row .$ExtensionSyncProperty )
117+ if ( ! $PatchObjects [ $ObjectUrl ][ $Props [ 0 ]].ContainsKey( $Props [ 1 ])) {
118+ Write-Host " Creating new object for $ ( $Props [ 1 ] ) "
119+ $PatchObjects [ $ObjectUrl ][ $Props [ 0 ]][ $Props [ 1 ]] = $Row .$ExtensionSyncProperty
97120 }
121+ } else {
122+ $PatchObjects [$ObjectUrl ][$CustomDataAttribute ] = $Row .$ExtensionSyncProperty
98123 }
99124 } elseif ($DatasetConfig.type -eq ' array' ) {
125+ Write-Warning " Processing array data for $ ( $CustomDataAttribute ) on $ ( $DirectoryObject.id ) - found $ ( $Row.Count ) entries"
126+ # Write-Information ($Row | ConvertTo-Json -Depth 10)
127+ if ($DatasetConfig.select ) {
128+ $Row = $Row | Select-Object - Property ($DatasetConfig.select -split ' ,' )
129+ }
100130
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- }
131+ if (! $PatchObjects [$ObjectUrl ].ContainsKey($CustomDataAttribute )) {
132+ $PatchObjects [$ObjectUrl ][$CustomDataAttribute ] = [System.Collections.Generic.List [string ]]::new()
107133 }
108134
109- $Body = [PSCustomObject ]@ {
110- $CustomDataAttribute = @ ($Data )
135+ $Data = if ($DatasetConfig.storeAs -eq ' json' ) {
136+ $Row | ConvertTo-Json - Depth 5 - Compress
137+ } else {
138+ $Row
111139 }
112- }
113140
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- })
141+ $PatchObjects [$ObjectUrl ][$CustomDataAttribute ].Add($Data )
142+ }
123143 }
124144 }
125145 }
126146
127- # Write-Host ($BulkRequests | ConvertTo-Json -Depth 10)
147+ foreach ($ObjectUrl in $PatchObjects.Keys ) {
148+ $PatchObject = $PatchObjects [$ObjectUrl ]
149+ $BulkRequests.Add ([PSCustomObject ]@ {
150+ id = ($ObjectUrl -split ' /' | Select-Object - Last 1 )
151+ url = $ObjectUrl
152+ method = ' PATCH'
153+ body = $PatchObject
154+ headers = @ {
155+ ' Content-Type' = ' application/json'
156+ }
157+ })
158+ }
159+
160+ Write-Host ($BulkRequests | ConvertTo-Json - Depth 10 )
128161 if ($BulkRequests.Count -gt 0 ) {
129162 Write-Information " Sending $ ( $BulkRequests.Count ) requests to Graph API"
130163 $Responses = New-GraphBulkRequest - tenantid $TenantFilter - Requests @ ($BulkRequests )
131164 if ($Responses | Where-Object { $_.statusCode -ne 204 }) {
132165 Write-Warning ' Some requests failed'
133- Write-Information ($Responses | Where-Object { $_.statusCode -ne 204 } | Select-Object - Property id , statusCode | ConvertTo-Json )
166+ Write-Information ($Responses | Where-Object { $_.status -ne 204 } | ConvertTo-Json - Depth 10 )
134167 }
135168 }
136169}
0 commit comments