SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Move Logins via SMO


Move Logins via SMO

Author
Message
JoseRocha
JoseRocha
Valued Member
Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)

Group: General Forum Members
Points: 74 Visits: 322
It's possible to create in Powershell, a script that move the login from a source server to a destination server, including the password ?
I tried to use the sp_help_revlogin procedure, but I'm unable to get the t-sql output into a variable.


$handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {param($sender, $event) $LoginDes | Write-Host $event.Message };
$smoSrc.connectionContext.add_InfoMessage($handler);
$smoSrc.connectionContext.executeNonQuery("EXEC master..sp_help_revlogin $dbLogin")



If a try to use the script() method, the code generated has the password field, but is not the sid value.


Tks
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10020 Visits: 13351
Reading the output stream from PoSh is a pain in the rear.
I would script the logins using TSQL instead:


SELECT 'CREATE LOGIN ' + QUOTENAME(name) + ' ' +
CASE type
WHEN 'S' THEN
'WITH PASSWORD = ' + CAST(CONVERT(varchar(256), CAST(LOGINPROPERTY(name,'PasswordHash') AS varbinary (256)), 1) AS nvarchar(max)) + ' HASHED ,
SID = ' + CAST(CONVERT(varchar(256), Sid, 1) AS nvarchar(max))
ELSE ' FROM WINDOWS '
END , type
FROM sys.server_principals
WHERE type IN ('S','U','G')



It may not be perfect, but it's a starting point.

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search