可以使用osql.exe或sqlcmd.exe来更改实例的默认密码策略吗?

时间:2013-05-20 15:49:14

标签: sql-server sql-server-2008

可以使用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的解决方案。

1 个答案:

答案 0 :(得分: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' 

GO