Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

create login  and user programtically using SMO sql 008

Posted on 2011-09-13
1
Medium Priority
?
417 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 25

Accepted Solution

by:
DBAduck - Ben Miller earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

598 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