< Back

Add-DbaServerRoleMember

Mon Jan 13, 2020 9:13 am

NAME Add-DbaServerRoleMember



SYNOPSIS

Adds a Database User to a database role for each instance(s) of SQL Server.





SYNTAX

Add-DbaServerRoleMember [[-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

[[-SqlCredential] <Pscredential>] [[-ServerRole] <System.String[]>] [[-Login] <System.String[]>] [[-Role]

<System.String[]>] [[-InputObject] <System.Object[]>] [-EnableException <Switch>] [<CommonParameters>]





DESCRIPTION

The Add-DbaServerRoleMember adds users in a database to a database role or roles for each instance(s) of SQL

Server.





PARAMETERS

-EnableException [<Switch>]

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.

This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables

advanced scripting.

Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own

try/catch.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-InputObject [<System.Object[]>]

Enables piped input from Get-DbaServerRole or New-DbaServerRole



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Login [<System.String[]>]

The login(s) to add to server-level role(s) specified.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Role [<System.String[]>]

The role(s) to add to server-level role(s) specified.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ServerRole [<System.String[]>]

The server-level role(s) to process.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlCredential [<Pscredential>]

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).



Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory -

Integrated are all supported.



For MFA support, please use Connect-DbaInstance.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the

function to be executed against multiple SQL Server instances.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Role, Security, Login

Author: Shawn Melton (@wsmelton)



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Add-DbaServerRoleMember -SqlInstance server1 -Role dbcreator -Login login1



Adds login1 to the server-level role dbcreator on the instance server1

-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Add-DbaServerRoleMember -SqlInstance server1, sql2016 -ServerRole customrole -Login login1



Adds login1 in custom, server-level role customrole on the instance server1 and sql2016

-------------------------- EXAMPLE 3 --------------------------



PS C:\\>Add-DbaServerRoleMember -SqlInstance server1 -ServerRole customrole -Role dbcreator



Adds custom, server-level role customrole to dbcreator server-level fixed role.

-------------------------- EXAMPLE 4 --------------------------



PS C:\\>$servers = Get-Content C:\\servers.txt



PS C:\\> $servers | Add-DbaServerRoleMember -ServerRole sysadmin -Login login1



Adds login1 to the sysadmin server-level role in every server in C:\\servers.txt

-------------------------- EXAMPLE 5 --------------------------



PS C:\\>Add-DbaServerRoleMember -SqlInstance localhost -ServerRole "bulkadmin","dbcreator" -Login login1



Adds login1 on the server localhost to the server-level roles bulkadmin and dbcreator

-------------------------- EXAMPLE 6 --------------------------



PS C:\\>$roles = Get-DbaServerRole -SqlInstance localhost -ServerRole "bulkadmin","dbcreator"



PS C:\\> $roles | Add-DbaServerRoleMember -Login login1



Adds login1 on the server localhost to the server-level roles bulkadmin and dbcreator

-------------------------- EXAMPLE 7 --------------------------



PS C:\\>PS C:\\ $logins = Get-Content C:\\logins.txt



PS C:\\ $srvLogins = Get-DbaLogin -SqlInstance server1 -Login $logins

PS C:\\ New-DbaServerRole -SqlInstance server1 -ServerRole mycustomrole -Owner sa | Add-DbaServerRoleMember -Login

$logins



Adds all the logins found in C:\\logins.txt to the newly created server-level role mycustomrole on server1.



RELATED LINKS

https://dbatools.io/Add-DbaServerRoleMember