Skip to content

Instantly share code, notes, and snippets.

@ctrl-freak
Created April 8, 2024 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrl-freak/922580419cceb1557cb9d237a5b562c6 to your computer and use it in GitHub Desktop.
Save ctrl-freak/922580419cceb1557cb9d237a5b562c6 to your computer and use it in GitHub Desktop.
Generate SQL to create MSSQL logins with same SID and password
-- SQL Server Logins
select
'create login [' + sp.[name] + '] with password=0x' + CONVERT(nvarchar(max), l.password_hash, 2) + ' hashed, sid=0x' + convert(nvarchar(2000), sp.[sid], 2) + ', default_language = [us_english];'
from
master.sys.server_principals sp
inner join master.sys.sql_logins l on sp.[sid] = l.[sid]
where
sp.type_desc = 'SQL_LOGIN'
and sp.is_disabled = 0
and sp.default_language_name = 'us_english' -- helps target the specifically created logins
order by
sp.[name]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment