Solved

create login  and user programtically using SMO sql 008

Posted on 2011-09-13
1
399 Views
Last Modified: 2012-06-27
How can I create login and user using SMO in sql 2008
I would like to create a program that creates login and user programtcally using SMO
if the login already exists ithen only create a user and give read only access to user
otherwise create new log and user and give the read only access to the dataabse

how can I do this using SMO sql 2008?
0
Comment
Question by:jung1975
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 24

Accepted Solution

by:
DBAduck - Ben Miller earned 500 total points
ID: 36536564
To create a login, you can see how to create it on http://powershellfordbas.com.  This is a generic creation, but you can also find out if it exists, by creating a server and checking the logins.



   
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList "servername"
$database = $server.Databases["databasename"]
if($server.Logins["loginname"] -eq $null) {
   $login = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList "servername", "loginname"
   $login.LoginType = "LoginType"  # SqlLogin, WindowsUser, WindowsGroup, Certificate, AssymetricKey
   $login.Create("Password")
}
else {
   $login = $server.Logins["loginname"]
}

$user = New-Object -TypeName Microsoft.SqlServer.Management.Smo.User -ArgumentList $database, "Username"
$user.Login = $login.Name
$user.AddToRole("db_datareader")
$user.Create()

Open in new window

0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question