I would like to add an existing local user to the SQL Server as a sysadmin, with PowerShell. fter some research I have the following script so far:
$Username = \
function SQL-Get-Server-Instance
{
param (
[parameter(Mandatory = $true)][string] $DatabaseServer,
[parameter(Mandatory = $true)][string] $InstanceName
)
if (!$InstanceName -or $InstanceName -eq "" -or $InstanceName -eq "MSSQLSERVER")
{ return $DatabaseServer }
else
{ return "$DatabaseServer\$InstanceName" }
}
function Create-SQL-Login
{
param (
[parameter(Mandatory = $true)][string] $loginName,
[parameter(Mandatory = $true)][string] $DatabaseServer,
[parameter(Mandatory = $false)][string] $InstanceName = "MSSQLSERVER"
)
$sqlConnection = $null
try
{
$Error.Clear()
$ServerInstance = SQL-Get-Server-Instance $DatabaseServer $InstanceName
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Server=$ServerInstance;Database=master;Trusted_Connection=True;"
$Command = New-Object System.Data.SqlClient.SqlCommand
$Command.CommandType = 1
$Command.Connection = $sqlConnection
$Command.CommandText = "create login [$loginName] from windows with default_database=[master], default_language=[us_english]"
$sqlConnection.Open()
$Command.ExecuteNonQuery() | Out-Null
$Command.CommandText = "exec master..sp_addsrvrolemember @loginame = N'$loginName', @rolename = N'sysadmin'"
$Command.ExecuteNonQuery() | Out-Null
}
catch
{
$str = (([string] $Error).Split(':'))[1]
Write-Error ($str.Replace('"', ''))
}
finally
{
if ($sqlConnection)
{ $sqlConnection.Close() }
}
}
And you call it like this:
Create-SQL-Login $env:COMPUTERNAME\JohnDoe $env:COMPUTERNAME
Change
'$Username'
with
"$Username"
Note that in powershell variable aren't expanded in single quote, then '$Username' is take as literal and not for the value of the variable.
I did not try your code. But, the following one worked for me on my SQL Express instance.
$conn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection -ArgumentList $env:ComputerName
$conn.applicationName = "PowerShell SMO"
$conn.ServerInstance = ".\SQLEXPRESS"
$conn.StatementTimeout = 0
$conn.Connect()
$smo = New-Object Microsoft.SqlServer.Management.Smo.Server -ArgumentList $conn
$SqlUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList $smo,"${env:ComputerName}\JohnDoe"
$SqlUser.LoginType = 'WindowsUser'
$sqlUser.PasswordPolicyEnforced = $false
$SqlUser.Create()
Found something simpler
Add-SqlLogin -ServerInstance <Server> -LoginName <User> -LoginType <LoginType> -DefaultDatabase tempdb -Enable -GrantConnectSql -LoginPSCredential $Credential
((New-Object ('Microsoft.SqlServer.Management.Smo.Server') "<Server>").Roles | where {$_.Name -eq 'sysadmin'}).AddMember("<User>")