Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions private/functions/Update-SqlPermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function Update-SqlPermission {
[ValidateNotNullOrEmpty()]
[object]$DestLogin,
[switch]$ObjectLevel,
[switch]$ExcludeDatabaseMapping,
[switch]$EnableException
)

Expand Down Expand Up @@ -194,6 +195,10 @@ function Update-SqlPermission {
}
}

if ($ExcludeDatabaseMapping) {
return
}

if ($DestServer.VersionMajor -lt 9) {
Write-Message -Level Warning -Message "SQL Server 2005 or greater required for database mappings.";
continue
Expand Down
15 changes: 14 additions & 1 deletion public/Copy-DbaLogin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ function Copy-DbaLogin {
Skips copying server roles, database permissions, and security mappings for the login accounts.
Use this when you only need the login accounts created but plan to configure permissions separately, or when copying logins for testing purposes.

.PARAMETER ExcludeDatabaseMapping
Skips copying database-level permissions and role memberships, syncing only server-level roles and securables.
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.

.PARAMETER SyncSaName
Renames the destination sa account to match the source sa account name if they differ.
Use this during migrations when your organization has renamed the sa account for security purposes and you need consistent naming across instances.
Expand Down Expand Up @@ -215,6 +219,7 @@ function Copy-DbaLogin {
[switch]$Force,
[switch]$ObjectLevel,
[switch]$ExcludePermissionSync,
[switch]$ExcludeDatabaseMapping,
[switch]$EnableException
)

Expand Down Expand Up @@ -508,7 +513,15 @@ function Copy-DbaLogin {
# 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
# So we loop, just in case...
foreach ($dl in $destLogin) {
Update-SqlPermission -SourceServer $sourceServer -SourceLogin $Login -DestServer $destServer -DestLogin $dl -ObjectLevel:$ObjectLevel
$splatPermission = @{
SourceServer = $sourceServer
SourceLogin = $Login
DestServer = $destServer
DestLogin = $dl
ObjectLevel = $ObjectLevel
ExcludeDatabaseMapping = $ExcludeDatabaseMapping
}
Update-SqlPermission @splatPermission
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Copy-DbaLogin.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Describe $CommandName -Tag UnitTests {
"LoginRenameHashtable",
"KillActiveConnection",
"ExcludePermissionSync",
"ExcludeDatabaseMapping",
"NewSid",
"ObjectLevel",
"Force",
Expand Down
Loading