Technical Article

Add users to Windows NT Group

,

This scripts adds the users to the specified Windows NT Group.

This will be helpful if we want to give the access to group of users to a sql server, which is runing with Windows Authentication. Create a NT group and give the access on SQL and run this script it adds the user into the Group.

/*
* Open Query Analyzer and connect to the Server on which users are to be added
* Replace <Database on Server> with Database name
* Replace Drive Name <h:\> to the relevant Drive
* Replace <ServerName> with Server Name
* Replace <Windows Group> with Windows Security Group to which users should be added
* Replace <Domain > with user's Domain name
* Replace user1 with actual user name
* Add more users one below the other
*/
USE <Database on Server>
GO
CREATE TABLE Buffer (data text)
GO

-- In the below script change the Drive name "h:\" to relevant Drive (Find and Replace)
INSERT Buffer VALUES(
'
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.OpenTextFile("h:\testread.txt")

strComputer = f1.ReadLine
strGroup = f1.ReadLine
strDomain = f1.ReadLine
Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup & ",group")

While NOT f1.AtEndOfStream
strUser = f1.ReadLine
wscript.echo "Adding " & strUser
objGroup.Add("WinNT://" & strDomain & "/" & strUser)
wend
f1.close
wscript.echo "Users are added successfully"
')
GO
exec Master.dbo.xp_cmdshell 'bcp "select * from <Database on Server>.dbo.Buffer" queryout h:\Test1.vbs -c'
GO

TRUNCATE TABLE Buffer
GO

INSERT Buffer VALUES
('<ServerName>
<Windows Group>
<Domain>
user1
user2')

exec Master.dbo.xp_cmdshell 'bcp "select * from <Database on Server - testerarea>.dbo.Buffer" queryout h:\Testread.txt -c'

exec Master.dbo.xp_cmdshell 'cScript h:\test1.vbs'

DROP TABLE Buffer

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating