?
Solved

create login  and user programtically using SMO sql 008

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

770 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