Wednesday, March 7, 2012

script - add SQL login

Hi everyone,
long time user, first time poster. hehe (always wanted to say that)
got a (hopefully) simple question.
I'm trying to script (vbscript, WMI or whatever works) adding a SQL
login so that my users can have access to SQL as Local Users on their
machines.
I would like to be able to add their username (%username%)'
but i would settle for adding the Domain Users group.
Any help or pointing in the right direction is very much appreciated.
Thanks everyone
Tim
tghetti@.air-worldwide.comYou can grant a Windows account access to SQL Server by executing the
sp_grantlogin stored procedure. Additionally, you can grant the users
access to your application database(s) and add the users to roles with
sp_grantdbaccess and sp_addrolemember.
The example script below can be run from Query Analyzer or the OSQL
command-line utility:
EXEC sp_grantlogin 'MyDomain\MyUser'
USE MyDatabase
EXEC sp_grantdbaccess 'MyDomain\MyUser'
EXEC sp_addrolemember 'MyRole', 'MyDomain\MyUser'
VbScript equivalent:
Dim connection, login
database = "MyDatabase"
login = "MyDomain\MyUser"
Set connection = CreateObject("ADODB.Connection")
connection.Open _
"Provider=SQLOLEDB;" & _
"Data Source=MyServer;" & _
"Integrated Security=SSPI;"
connection.Execute "EXEC sp_grantlogin '" & login & "'"
connection.Execute "USE " & database
connection.Execute "EXEC sp_grantdbaccess '" & login & "'"
connection.Execute "EXEC sp_addrolemember 'MyRole', '" & login & "'"
connection.Close
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Tim" <Tim@.Directinsight.biz> wrote in message
news:e4f1004c.0408020947.19a29b4d@.posting.google.com...
> Hi everyone,
> long time user, first time poster. hehe (always wanted to say that)
> got a (hopefully) simple question.
> I'm trying to script (vbscript, WMI or whatever works) adding a SQL
> login so that my users can have access to SQL as Local Users on their
> machines.
> I would like to be able to add their username (%username%)'
> but i would settle for adding the Domain Users group.
> Any help or pointing in the right direction is very much appreciated.
> Thanks everyone
> Tim
> tghetti@.air-worldwide.com|||Thanks Dan...your the man
Tim
"Dan Guzman" <danguzman@.nospam-earthlink.net> wrote in message news:<uOy25NPeEHA.1764@.TK2MSFTNGP10.phx.gbl>...
> You can grant a Windows account access to SQL Server by executing the
> sp_grantlogin stored procedure. Additionally, you can grant the users
> access to your application database(s) and add the users to roles with
> sp_grantdbaccess and sp_addrolemember.
> The example script below can be run from Query Analyzer or the OSQL
> command-line utility:
> EXEC sp_grantlogin 'MyDomain\MyUser'
> USE MyDatabase
> EXEC sp_grantdbaccess 'MyDomain\MyUser'
> EXEC sp_addrolemember 'MyRole', 'MyDomain\MyUser'
> VbScript equivalent:
> Dim connection, login
> database = "MyDatabase"
> login = "MyDomain\MyUser"
> Set connection = CreateObject("ADODB.Connection")
> connection.Open _
> "Provider=SQLOLEDB;" & _
> "Data Source=MyServer;" & _
> "Integrated Security=SSPI;"
> connection.Execute "EXEC sp_grantlogin '" & login & "'"
> connection.Execute "USE " & database
> connection.Execute "EXEC sp_grantdbaccess '" & login & "'"
> connection.Execute "EXEC sp_addrolemember 'MyRole', '" & login & "'"
> connection.Close
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Tim" <Tim@.Directinsight.biz> wrote in message
> news:e4f1004c.0408020947.19a29b4d@.posting.google.com...
> > Hi everyone,
> >
> > long time user, first time poster. hehe (always wanted to say that)
> >
> > got a (hopefully) simple question.
> >
> > I'm trying to script (vbscript, WMI or whatever works) adding a SQL
> > login so that my users can have access to SQL as Local Users on their
> > machines.
> > I would like to be able to add their username (%username%)'
> > but i would settle for adding the Domain Users group.
> >
> > Any help or pointing in the right direction is very much appreciated.
> >
> > Thanks everyone
> >
> > Tim
> > tghetti@.air-worldwide.com

No comments:

Post a Comment