PowerShell for Creating a Database Login
This is PowerShell tutorial script to walk you through how to create database login with the help of PowerShell and SMO.
Here is simple way was can achieve this with the help of T-SQL:
CREATE LOGIN [bi-bigdata] WITH PASSWORD=N'YourPasswordAsPerPolicy', CHECK_EXPIRATION=OFF GO
But this of that we do not have SQL Server Management Studio with you and you want to create login or another scenario where you are going to create database login with some sort of automated script. Following simple PowerShell script is really going to help you out there.
#import SQL Server module Import-Module SQLPS -DisableNameChecking #your SQL Server Instance Name $SQLInstanceName = "SQL2012" $Server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $SQLInstanceName #Specify Database Login Name $Login_Name = "bi-bigdata" #Drop Login if exists if ($Server.Logins.Contains($Login_Name)) { $Server.Logins[$Login_Name].Drop() } #Create an SMO Login object $Login = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList $Server, $Login_Name #identify what type of login this is. #The possible LoginTypes are AsymmetricKey, Certificate, SQLLogin, WindowsGroup, and WindowsUser #We are using using a SQLLogin for this turorial $Login.LoginType = [Microsoft.SqlServer.Management.Smo.LoginType]::SqlLogin #Login object also has a few properties, such as PasswordPolicyEnforced and PasswordExpirationEnabled. $Login.PasswordExpirationEnabled = $false #Prompt the user for the password instead of hardcoding it #collect Password using a Read-Host cmdlet. $Password = Read-Host "PW" –AsSecureString $Login.Create($Password)
That’s it. In next article I’ll write about how to assign permissions and roles to a login existing database login.
Stay tuned…
Sandip
Categories: Powershell, SQL, SQL Server
database, login, powershell
Nice weblog right here! Also your web site
so much up very fast! What web host are you the usage of?
Can I get your affiliate link in your host? I wish my web site loaded up as
fast as yours lol
How do i create multiple users in a database of sql server 2012
This is the right web site for anyone who wants to understand this topic. You know so much its almost hard to argue with you (not that I personally will need to…HaHa). You certainly put a new spin on a topic which has been discussed for ages. Wonderful stuff, just great!
You should take part in a contest for one of the greatest blogs on the net. I’m going to highly recommend this blog!
You can certainly see your enthusiasm within the work you write. The sector hopes for even more passionate writers such as you who are not afraid to mention how they believe. All the time go after your heart.
Great post. I was checking constantly this blog and I’m impressed! Extremely useful info particularly the last part 🙂 I care for such info much. I was seeking this certain info for a long time. Thank you and best of luck.