2

我需要编写一个脚本来提取 SQL 登录的密码并将其传播到另一台服务器。

我这样做了:

Declare @PasswordBinary varbinary(256)
Set @PasswordBinary = CAST(LOGINPROPERTY( @LoginName, 'PasswordHash' ) AS varbinary (256))

Exec sp_hexadecimal @PasswordBinary, @PasswordStr Out

sp_hexadecimal :

Create PROCEDURE [dbo].[sp_hexadecimal]
    @binvalue varbinary(256),
    @hexvalue varchar (514) OUTPUT
AS
  DECLARE @charvalue varchar (514)
  DECLARE @i int
  DECLARE @length int
  DECLARE @hexstring char(16)

  SELECT @charvalue = '0x'
  SELECT @i = 1
  SELECT @length = DATALENGTH (@binvalue)
  SELECT @hexstring = '0123456789ABCDEF'

  WHILE (@i <= @length)
  BEGIN
     DECLARE @tempint int
     DECLARE @firstint int
     DECLARE @secondint int

     SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1))
     SELECT @firstint = FLOOR(@tempint/16)
     SELECT @secondint = @tempint - (@firstint*16)

     SELECT @charvalue = @charvalue +
                         SUBSTRING(@hexstring, @firstint+1, 1) +
                         SUBSTRING(@hexstring, @secondint+1, 1)

     SELECT @i = @i + 1
  END

  SELECT @hexvalue = @charvalue

我有一个变量

@SQL nvarchar(max) = 'CREATE LOGIN ' + QUOTENAME(@LoginName) + ' With PASSWORD = ' + @PasswordStr + ' HASHED'''

我将它发送到远程服务器上的存储过程,并在那里执行这个变量:

 Exec (@SQL)

但我得到一个错误:

@SQL 变量设置为:'Create Login [Name] With password = 0x Hashed'

谢谢 :)

4

2 回答 2

2

更改您的程序以在密码上添加引号:

SET @tmpstr = 'CREATE LOGIN [' + @name + '] WITH PASSWORD=' + @txtpwd + ' HASHED' 
于 2012-04-18T12:32:26.187 回答
1

这对我有用

SELECT  'create login ' + QUOTENAME(name) + ' with password = '
        + CONVERT(SYSNAME, [password_hash], 1) + ' hashed'
FROM    sys.[sql_logins]
于 2012-04-18T20:49:23.953 回答