This recipe shows how you can create a login using PowerShell and SMO.
For this recipe, we will create a SQL login called eric
. The T-SQL equivalent of what we are trying to accomplish is:
CREATE LOGIN [eric] WITH PASSWORD=N'YourSuperStrongPassword', CHECK_EXPIRATION=OFF GO
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Import the
SQLPS
module and create a new SMO Server object as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "KERRIGAN" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run it:
$loginName = "eric" # drop login if it exists if ($server.Logins.Contains($loginName)) { $server.Logins[$loginName].Drop() } $login = New-Object ` -TypeName Microsoft.SqlServer.Management.Smo.Login ` -ArgumentList $server...