Skip to content

Commit 6d56b81

Browse files
Copy-DbaLogin - Add -ExcludeDatabaseMapping to sync only server permissions
Adds a new -ExcludeDatabaseMapping switch to Copy-DbaLogin that syncs only server-level roles and securables, skipping the database mapping loop. This addresses the performance concern for instances with many databases where full permission sync is slow but -ExcludePermissionSync discards desired server-level permissions. Fixes #8312 (do Copy-DbaLogin) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
1 parent 97d03be commit 6d56b81

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

private/functions/Update-SqlPermission.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function Update-SqlPermission {
3232
[ValidateNotNullOrEmpty()]
3333
[object]$DestLogin,
3434
[switch]$ObjectLevel,
35+
[switch]$ExcludeDatabaseMapping,
3536
[switch]$EnableException
3637
)
3738

@@ -194,6 +195,10 @@ function Update-SqlPermission {
194195
}
195196
}
196197

198+
if ($ExcludeDatabaseMapping) {
199+
return
200+
}
201+
197202
if ($DestServer.VersionMajor -lt 9) {
198203
Write-Message -Level Warning -Message "SQL Server 2005 or greater required for database mappings.";
199204
continue

public/Copy-DbaLogin.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ function Copy-DbaLogin {
4646
Skips copying server roles, database permissions, and security mappings for the login accounts.
4747
Use this when you only need the login accounts created but plan to configure permissions separately, or when copying logins for testing purposes.
4848
49+
.PARAMETER ExcludeDatabaseMapping
50+
Skips copying database-level permissions and role memberships, syncing only server-level roles and securables.
51+
Use this when you want to sync server permissions (sysadmin membership, server securables, etc.) without iterating through all databases, which significantly improves performance on instances with many databases.
52+
4953
.PARAMETER SyncSaName
5054
Renames the destination sa account to match the source sa account name if they differ.
5155
Use this during migrations when your organization has renamed the sa account for security purposes and you need consistent naming across instances.
@@ -215,6 +219,7 @@ function Copy-DbaLogin {
215219
[switch]$Force,
216220
[switch]$ObjectLevel,
217221
[switch]$ExcludePermissionSync,
222+
[switch]$ExcludeDatabaseMapping,
218223
[switch]$EnableException
219224
)
220225

@@ -508,7 +513,15 @@ function Copy-DbaLogin {
508513
# In rare cases, when the instance has a case sensitive collation and there are two logins that differ only in case, New-DbaLogin will return them both into $destLogin
509514
# So we loop, just in case...
510515
foreach ($dl in $destLogin) {
511-
Update-SqlPermission -SourceServer $sourceServer -SourceLogin $Login -DestServer $destServer -DestLogin $dl -ObjectLevel:$ObjectLevel
516+
$splatPermission = @{
517+
SourceServer = $sourceServer
518+
SourceLogin = $Login
519+
DestServer = $destServer
520+
DestLogin = $dl
521+
ObjectLevel = $ObjectLevel
522+
ExcludeDatabaseMapping = $ExcludeDatabaseMapping
523+
}
524+
Update-SqlPermission @splatPermission
512525
}
513526
}
514527
}

tests/Copy-DbaLogin.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Describe $CommandName -Tag UnitTests {
2424
"LoginRenameHashtable",
2525
"KillActiveConnection",
2626
"ExcludePermissionSync",
27+
"ExcludeDatabaseMapping",
2728
"NewSid",
2829
"ObjectLevel",
2930
"Force",

0 commit comments

Comments
 (0)