From 0302286505a9533438341ce8b4a5be4fdd582ac9 Mon Sep 17 00:00:00 2001 From: Tomas Kracmar Date: Tue, 24 Nov 2026 12:27:16 +0100 Subject: [PATCH] feat(import): create only the groups and filters an import references With Import assignments, migration-table groups are no longer all looked up or created up front. They are resolved on demand in Update-JsonForEnvironment, so only groups referenced by imported objects are created in the target tenant. Assignment filters that exist in the export but not in the target are created on demand too (needs Replace dependency ids). Synced source groups log a warning and are created as cloud groups. New groups get a random 10-character mailNickname like the Entra portal; group lookup escapes apostrophes in names. --- Extensions/MSGraph.psm1 | 203 ++++++++++++++++++++++++---------------- 1 file changed, 123 insertions(+), 80 deletions(-) diff --git a/Extensions/MSGraph.psm1 b/Extensions/MSGraph.psm1 index 05d4fc6..69ec7a4 100644 --- a/Extensions/MSGraph.psm1 +++ b/Extensions/MSGraph.psm1 @@ -3171,91 +3171,92 @@ function Get-GraphMigrationObjectsFromFile if($global:chkImportAssignments.IsChecked -eq $true) { - # Only check groups if Assignments are imported - # This will CREATE the group if it doesn't exist in the target environment + # Register the groups only. They are looked up or created in the target tenant on demand + # (see Update-JsonForEnvironment), so only groups that an imported object really references are created. foreach($migObj in $migFileObj.Objects) { if($migObj.Type -like "*group*") - { - $migTableGroupName = $migObj.DisplayName.Trim() - $originalGroupName = $migTableGroupName - if($txtSearchPattern -and $migTableGroupName -match $txtSearchPattern) - { - $migTableGroupName = $migTableGroupName -replace $txtSearchPattern, $txtReplacePattern - } - $obj = (Invoke-GraphRequest "/groups?`$filter=displayName eq '$($migTableGroupName)'").Value - if(-not $obj) - { - $groupFi = $null - if($global:GraphMigrationTable) - { - $fi = [IO.FileInfo]$global:GraphMigrationTable - $groupFi = [IO.FileInfo](Join-Path (Join-Path $fi.DirectoryName "Groups") "$((Remove-InvalidFileNameChars $originalGroupName)).json") - } - - if($groupFi.Exists -eq $true) - { - # ToDo: Create group from Json (could be a dynamic group) - # Warn if synched group - $groupObj = Get-GraphObjectFromFile $groupFi.FullName - - #isAssignableToRole - For Role assignment groupd. - $keepProps = @("displayName","description","mailEnabled","mailNickname","securityEnabled","membershipRule","groupTypes", "membershipRuleProcessingState") - foreach($prop in $groupObj.PSObject.Properties) - { - if($prop.Name -in $keepProps) { continue } - - Remove-Property $groupObj $prop.Name - } - $groupObj.displayName = $groupObj.displayName.Trim() - if($txtSearchPattern -and $groupObj.displayName -match $txtSearchPattern) - { - $groupObj.displayName = $groupObj.displayName -replace $txtSearchPattern, $txtReplacePattern - } - $groupJson = ConvertTo-Json $groupObj -Depth 50 - } - else - { - $groupName = $migTableGroupName - Write-Log "No group object found for $groupName. Creating a cloud group with default settings" 2 - $dateStr = ((Get-Date).ToString("yyMMddHHmmss")) - - if(($groupName.Length + $dateStr.Length) -gt 64) - { - $nickName = $groupName.Substring(0,(64-$dateStr.Length)) - } - else - { - $nickName = $groupName - } - $nickName = $nickName + $dateStr - - $groupJson = @" - { - "displayName": "$($groupName)", - "mailEnabled": false, - "mailNickname": "$($nickName)", - "securityEnabled": true - } -"@ - } - Write-Log "Create AAD Group $($migTableGroupName)" - - $obj = Invoke-GraphRequest "/groups" -HttpMethod "POST" -Content $groupJson - } - - if($obj) - { - $global:MigrationTableCache += (New-Object PSObject -Property @{ - OriginalId = $migObj.Id - Id = $obj.Id - Type = $migObj.Type - }) - } + { + $global:MigrationTableCache += (New-Object PSObject -Property @{ + OriginalId = $migObj.Id + Id = $null + Type = $migObj.Type + Pending = $true + MigObj = $migObj + SearchPattern = $txtSearchPattern + ReplacePattern = $txtReplacePattern + }) } } } } + +# Find the group in the target tenant by name or create it. Returns the group object or $null +function Resolve-GraphMigrationGroup +{ + param($migObj, $txtSearchPattern, $txtReplacePattern) + + $migTableGroupName = $migObj.DisplayName.Trim() + $originalGroupName = $migTableGroupName + if($txtSearchPattern -and $migTableGroupName -match $txtSearchPattern) + { + $migTableGroupName = $migTableGroupName -replace $txtSearchPattern, $txtReplacePattern + } + + $obj = (Invoke-GraphRequest "/groups?`$filter=displayName eq '$($migTableGroupName.Replace("'","''"))'").Value + if($obj) + { + return ($obj | Select-Object -First 1) + } + + $groupFi = $null + if($global:GraphMigrationTable) + { + $fi = [IO.FileInfo]$global:GraphMigrationTable + $groupFi = [IO.FileInfo](Join-Path (Join-Path $fi.DirectoryName "Groups") "$((Remove-InvalidFileNameChars $originalGroupName)).json") + } + + if($groupFi.Exists -eq $true) + { + # ToDo: Create group from Json (could be a dynamic group) + $groupObj = Get-GraphObjectFromFile $groupFi.FullName + + if($groupObj.onPremisesSyncEnabled -eq $true) + { + Write-Log "Group '$originalGroupName' is synced from on-premises AD in the source tenant. Creating it as a cloud group" 2 + } + + #isAssignableToRole - For Role assignment groupd. + $keepProps = @("displayName","description","mailEnabled","mailNickname","securityEnabled","membershipRule","groupTypes", "membershipRuleProcessingState") + foreach($prop in $groupObj.PSObject.Properties) + { + if($prop.Name -in $keepProps) { continue } + + Remove-Property $groupObj $prop.Name + } + $groupObj.displayName = $migTableGroupName + $groupJson = ConvertTo-Json $groupObj -Depth 50 + } + else + { + Write-Log "No group object found for $migTableGroupName. Creating a cloud group with default settings" 2 + # Random nickname like the Entra portal creates + $nickName = (New-Guid).Guid.SubString(0,10) + + $groupJson = @" + { + "displayName": "$($migTableGroupName)", + "mailEnabled": false, + "mailNickname": "$($nickName)", + "securityEnabled": true + } +"@ + } + + Write-Log "Create AAD Group $($migTableGroupName)" + Invoke-GraphRequest "/groups" -HttpMethod "POST" -Content $groupJson +} + function Update-JsonForEnvironment { param($json) @@ -3269,8 +3270,19 @@ function Update-JsonForEnvironment { foreach($depObj in $global:LoadedDependencyObjects[$depObjType]) { - if(-not $depObj.Id -or -not $depObj.OriginalId) { continue } + if(-not $depObj.OriginalId) { continue } if($depObj.OriginalId.Length -lt 36) { continue } # Skip non-guid IDs # ToDo: Verify... + if($json.IndexOf($depObj.OriginalId,[StringComparison]::OrdinalIgnoreCase) -lt 0) { continue } + + if(-not $depObj.Id -and $depObj.Pending -and -not $depObj.Failed) + { + # Object exists in the export but not in the target tenant. Create it now that something uses it + Write-Log "Create $($depObj.Type) '$($depObj.Name)' in target tenant (referenced by imported object)" + $newDepObj = Import-GraphObject $depObj.FileObj.Object $depObj.ObjectType $depObj.FileObj.FileInfo.FullName + if($newDepObj -and $newDepObj.Id) { $depObj.Id = $newDepObj.Id } else { $depObj.Failed = $true } + } + + if(-not $depObj.Id) { continue } $json = $json -replace $depObj.OriginalId,$depObj.Id } } @@ -3281,8 +3293,18 @@ function Update-JsonForEnvironment # Enumerate all objects in the migration table and replace all exported Id's to Id's in the new environment foreach($migInfo in ($global:MigrationTableCache | Where Type -like "*group*")) { - if(-not $migInfo.Id -or -not $migInfo.OriginalId) { continue } + if(-not $migInfo.OriginalId) { continue } if($migInfo.OriginalId.Length -lt 36) { continue } # Skip non-guid IDs # ToDo: Verify... + if($json.IndexOf($migInfo.OriginalId,[StringComparison]::OrdinalIgnoreCase) -lt 0) { continue } + + if(-not $migInfo.Id -and $migInfo.Pending -and -not $migInfo.Failed) + { + # Only look up or create the group in the target tenant when an imported object references it + $tmpGroup = Resolve-GraphMigrationGroup $migInfo.MigObj $migInfo.SearchPattern $migInfo.ReplacePattern + if($tmpGroup -and $tmpGroup.Id) { $migInfo.Id = $tmpGroup.Id } else { $migInfo.Failed = $true } + } + + if(-not $migInfo.Id) { continue } $json = $json -replace $migInfo.OriginalId,$migInfo.Id } @@ -3396,6 +3418,27 @@ function Add-GraphDependencyObjects } } + if($depObjectType.Id -eq "AssignmentFilters") + { + # Exported filters that do not exist in the target tenant. Created on demand, only when an imported object references them + foreach($depFile in $depFiles) + { + $depFileName = Get-GraphObjectName $depFile.Object $depObjectType + if($depFile.Object.Id -and -not ($arrDepObjects | Where { $_.OriginalId -eq $depFile.Object.Id }) -and -not ($depObjects | Where { (Get-GraphObjectName $_ $depObjectType) -eq $depFileName })) + { + $arrDepObjects += New-Object PSObject -Property @{ + OriginalId = $depFile.Object.Id + Name = $depFileName + Id = $null + Type = $depObjectType.Id + Pending = $true + FileObj = $depFile + ObjectType = $depObjectType + } + } + } + } + if($arrDepObjects.Count -gt 0) { $global:LoadedDependencyObjects.Add($depObjectType.Id,$arrDepObjects)