Move Logins via SMO

  • 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

  • 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

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply