1

可以使用 osql.exe 或 sqlcmd.exe 更改实例的密码策略吗?我有一个创建数据库和用户的批处理脚本,但如您所知,“sp_addlogin”存储过程在创建用户时似乎没有允许少于 8 个字符密码的选项。 在使用“sp_addlogin”创建登录之前,如何使用调用 osql 的批处理脚本来降低此策略要求?

到目前为止,我是这样做的:

-- CREATE USER 2005/2008 sql generated from batch file
-- where username and db name are the same
IF NOT EXISTS (SELECT * FROM master.dbo.syslogins WHERE loginname = N'teee') 
BEGIN 
  declare @loginlang nvarchar(132) 
  SELECT @loginlang = N'us_english' 
  IF @loginlang IS NULL OR (NOT EXISTS (SELECT * FROM master.dbo.syslanguages WHERE name = @loginlang) and @loginlang != N'us_english') 
    SELECT @loginlang = @@language 
 EXEC sp_addlogin N'teee', N'PassMe', N'TEEE', @loginlang 
 ALTER LOGIN teee WITH PASSWORD = 'PassMe' UNLOCK, CHECK_POLICY = OFF, CHECK_EXPIRATION = OFF 
END 
GO 
IF NOT EXISTS (SELECT * FROM dbo.sysusers WHERE name = N'teee' and status != 0) 
EXEC sp_grantdbaccess N'teee', N'teee' 
GO 
EXEC sp_addrolemember N'db_datareader', N'teee' 
GO 
EXEC sp_addrolemember N'db_datawriter', N'teee' 
GO 
EXEC sp_addrolemember N'db_owner', N'teee' 
GO 

运行此程序时出现的错误是:

Msg 15116, Level 16, State 1, Server U0163499, Line 1
Password validation failed. The password does not meet Windows policy requirements because it is too short.
Msg 15151, Level 16, State 1, Server U0163499, Line 10
Cannot alter the login 'teee', because it does not exist or you do not have permission.

显然,如果我在创建用户时使用的密码大于或等于 8 个字符,它就可以正常工作。我需要一个也适用于 SQL 2012 的解决方案。

4

1 回答 1

1

你的代码

 EXEC sp_addlogin N'teee', N'PassMe', N'TEEE', @loginlang 
 ALTER LOGIN teee WITH PASSWORD = 'PassMe' UNLOCK, CHECK_POLICY = OFF,
 CHECK_EXPIRATION = OFF 

将上面的行替换为下面的行:

USE [master]
GO
CREATE LOGIN [teee] WITH PASSWORD=N'PassMe', 
DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO

这段代码对我有用

IF NOT EXISTS (SELECT * FROM master.dbo.syslogins WHERE loginname = N'teee') 
BEGIN 
  declare @loginlang nvarchar(132) 
  SELECT @loginlang = N'us_english' 
  IF @loginlang IS NULL OR (NOT EXISTS (
  SELECT * FROM master.dbo.syslanguages WHERE name = @loginlang)
   and @loginlang != N'us_english') 
    SELECT @loginlang = @@language 

 --EXEC sp_addlogin N'teee', N'PassMe', N'TEEE', @loginlang 
 --ALTER LOGIN teee WITH PASSWORD = 'PassMe' UNLOCK,
 --CHECK_POLICY = OFF, CHECK_EXPIRATION = OFF 

 CREATE LOGIN [teee] WITH PASSWORD=N'PassMe', 
 DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
END 
GO 
IF NOT EXISTS (SELECT * FROM dbo.sysusers WHERE name = N'teee' and status != 0) 
EXEC sp_grantdbaccess N'teee', N'teee' 
GO 
EXEC sp_addrolemember N'db_datareader', N'teee' 
GO 
EXEC sp_addrolemember N'db_datawriter', N'teee' 
GO 
EXEC sp_addrolemember N'db_owner', N'teee' 

于 2013-05-20T16:46:10.373 回答