-
-
Notifications
You must be signed in to change notification settings - Fork 852
Sync-DbaLoginSid - Add command to synchronize SQL login SIDs across instances #9955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…nstances (do Sync-DbaLoginSid) Co-authored-by: Chrissy LeMaire <[email protected]>
Sync-DbaLoginSid now retrieves and preserves the destination login's password hash, server roles, and properties when syncing the SID. The login is dropped and recreated with the original password, SID, default database, language, policy settings, and roles, ensuring a seamless migration. (do Sync-DbaLoginSid)
Code Review: Sync-DbaLoginSidThank you for this comprehensive PR! This addresses a real pain point (#3679) with SID mismatches. I've completed a thorough review and found one critical issue that must be fixed before merging. 🚨 CRITICAL ISSUE - Command Won't WorkProblem: The command references Looking at the diff, I can see two different implementations in the file:
SQL Server does NOT support Evidence from the DiffThe grep output shows: # Convert SID to hex string for ALTER LOGIN statement
$sql = "ALTER LOGIN [$loginName] WITH SID = $sidHex"This will fail - SQL Server will return an error like: The Correct Approach (Already in Your Code!)The DROP/CREATE pattern shown in lines 211-268 is the correct approach and should be the only implementation:
What Needs to be FixedRemove the ✅ Style & Convention ReviewThe code otherwise follows dbatools standards well: Excellent Adherence to CLAUDE.md
Code Quality
Test Coverage
🔍 Additional ObservationsMinor: Missing Input ValidationWhen using if ($InputObject) {
$sourceLogins = $InputObject | Where-Object { $_.PSObject.TypeNames -match 'Microsoft.SqlServer.Management.Smo.Login' -and $_.LoginType -eq 'SqlLogin' }
}Security Consideration: Password Hash ExposureThe
Performance: Multiple Destination InstancesWhen syncing to multiple destinations, consider that each destination connects sequentially. This is fine for the use case, but worth documenting that syncing to 20 servers will take time. Documentation: Database User MappingsThe help text mentions "preserving database mappings" but the implementation focuses on server-level properties. After DROP/CREATE, database user mappings (the actual orphaned users) remain orphaned until the SID matches. This is expected behavior, but consider clarifying:
Actually, re-reading the PR description - this is precisely the point! The SID sync fixes the root cause so database users are no longer orphaned. This is correct. 📋 Action ItemsRequired Before Merge
Optional Improvements
🎯 SummaryOverall Assessment: This is a well-designed, well-tested command that solves a real problem. The code quality is excellent and follows all dbatools conventions. Blocking Issue: The Recommendation: Fix the critical issue, then this is ready to merge. Great work on addressing a 6+ year old issue! CLAUDE.md Compliance Score: 9.5/10 (would be 10/10 after fixing the ALTER LOGIN issue) Test Coverage: Excellent - comprehensive scenarios covered Documentation: Excellent - clear, detailed help with great examples |
|
mmmmmm no |
Not convinced by this, anyone else chime in.
Sync-DbaLoginSid - Synchronize SQL Login SIDs Across Instances
Problem Solved
Fixes a long-standing issue (#3679, opened 2018) where SQL Server environments have the same login with different SIDs across multiple servers. This creates ongoing operational pain:
Repair-DbaDbOrphanUserevery timeWhy Existing Commands Don't Solve This
Copy-DbaLogin -Forcewas suggested in the original issue discussion, but it has critical limitations:The use case requires different passwords per environment (prod vs dev/test) while aligning SIDs to prevent orphaned users.
Implementation Details
Technical Approach
Since SQL Server provides no way to
ALTERa login's SID (theLogin.SidSMO property is read-only after creation), the command:This surgical approach changes only the SID while preserving everything else.
Key Features
Get-DbaLoginoutputConfirmImpact = "High"with full WhatIf supportReal-World Use Cases
Testing
Comprehensive test suite includes:
Code Quality
::new()syntax)= $trueattributes)dbatools.psd1anddbatools.psm1Closes
Closes #3679 (opened June 2018)