commands

^

Export-DbaServerRole

Author Patrick Flynn (@sqllensman)
Availability Windows, Linux, macOS

 

Synopsis

Exports server roles to a T-SQL file. Export includes Role creation, object permissions and Schema ownership.

Description

Exports Server roles to a T-SQL file. Export includes Role creation, object permissions and Role Members

Applies mostly to SQL Server 2012 or Higher when user defined Server roles were added but can be used on earlier versions to get role members. This command is an extension of John Eisbrener's post "Fully Script out a MSSQL Database Role" Reference: https://dbaeyes.wordpress.com/2013/04/19/fully-script-out-a-mssql-database-role/

Syntax

Export-DbaServerRole
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-InputObject] <Object[]>]
    [[-ScriptingOptionsObject] <ScriptingOptions>]
    [[-ServerRole] <String[]>]
    [[-ExcludeServerRole] <String[]>]
    [-ExcludeFixedRole]
    [-IncludeRoleMember]
    [[-Path] <String>]
    [[-FilePath] <String>]
    [-Passthru]
    [[-BatchSeparator] <String>]
    [-NoClobber]
    [-Append]
    [-NoPrefix]
    [[-Encoding] <String>]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Export-DbaServerRole -SqlInstance sql2005

Exports the Server Roles for SQL Server "sql2005" and writes them to the path defined in the ConfigValue 'Path.DbatoolsExport' using a a default name pattern of ServerName-YYYYMMDDhhmmss-serverrole.
Uses BatchSeparator defined by Config 'Formatting.BatchSeparator'

Example: 2
PS C:\> Export-DbaServerRole -SqlInstance sql2005 -Path C:\temp

Exports the Server Roles for SQL Server "sql2005" and writes them to the path "C:\temp" using a a default name pattern of ServerName-YYYYMMDDhhmmss-serverrole. Uses BatchSeparator defined by Config
'Formatting.BatchSeparator'

Example: 3
PS C:\> Export-DbaServerRole -SqlInstance sqlserver2014a -FilePath C:\temp\ServerRoles.sql

Exports the Server Roles for SQL Server sqlserver2014a to the file C:\temp\ServerRoles.sql. Overwrites file if exists

Example: 4
PS C:\> Export-DbaServerRole -SqlInstance sqlserver2014a -ServerRole SchemaReader -Passthru

Exports ONLY ServerRole SchemaReader FROM sqlserver2014a and writes script to console

Example: 5
PS C:\> Export-DbaServerRole -SqlInstance sqlserver2008 -ExcludeFixedRole -ExcludeServerRole Public -IncludeRoleMember -FilePath C:\temp\ServerRoles.sql -Append -BatchSeparator ''

Exports server roles from sqlserver2008, excludes all roles marked as as FixedRole and Public role. Includes RoleMembers and writes to file C:\temp\ServerRoles.sql, appending to file if it exits.
Does not include a BatchSeparator

Example: 6
PS C:\> Get-DbaServerRole -SqlInstance sqlserver2012, sqlserver2014  | Export-DbaServerRole

Exports server roles from sqlserver2012, sqlserver2014 and writes them to the path defined in the ConfigValue 'Path.DbatoolsExport' using a a default name pattern of
ServerName-YYYYMMDDhhmmss-serverrole

Example: 7
PS C:\> Get-DbaServerRole -SqlInstance sqlserver2016 -ExcludeFixedRole -ExcludeServerRole Public | Export-DbaServerRole -IncludeRoleMember

Exports server roles from sqlserver2016, excludes all roles marked as as FixedRole and Public role. Includes RoleMembers

Optional Parameters

-SqlInstance

The target SQL Server instance or instances. SQL Server 2000 and above supported.

Alias
Required False
Pipeline false
Default Value
-SqlCredential

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.

Alias
Required False
Pipeline false
Default Value
-InputObject

Enables piping from Get-DbaServerRole

Alias
Required False
Pipeline true (ByValue)
Default Value
-ScriptingOptionsObject

An SMO Scripting Object that can be used to customize the output - see New-DbaScriptingOption

Alias
Required False
Pipeline false
Default Value
-ServerRole

Server-Level role(s) to filter results to that role only.

Alias
Required False
Pipeline false
Default Value
-ExcludeServerRole

Server-Level role(s) to exclude from results.

Alias
Required False
Pipeline false
Default Value
-ExcludeFixedRole

Filter the fixed server-level roles. As only SQL Server 2012 or higher supports creation of server-level roles will eliminate all output for earlier versions.

Alias
Required False
Pipeline false
Default Value False
-IncludeRoleMember

Include scripting of role members in script

Alias
Required False
Pipeline false
Default Value False
-Path

Specifies the directory where the file or files will be exported. Will default to Path.DbatoolsExport Configuration entry

Alias
Required False
Pipeline false
Default Value (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport')
-FilePath

Specifies the full file path of the output file. If left blank then filename based on Instance name, Database name and date is created. If more than one database or instance is input then this parameter should normally be blank.

Alias OutFile,FileName
Required False
Pipeline false
Default Value
-Passthru

Output script to console only

Alias
Required False
Pipeline false
Default Value False
-BatchSeparator

Batch separator for scripting output. Uses the value from configuration Formatting.BatchSeparator by default. This is normally "GO"

Alias
Required False
Pipeline false
Default Value (Get-DbatoolsConfigValue -FullName 'Formatting.BatchSeparator')
-NoClobber

If this switch is enabled, a file already existing at the path specified by Path will not be overwritten. This takes precedence over Append switch

Alias
Required False
Pipeline false
Default Value False
-Append

If this switch is enabled, content will be appended to a file already existing at the path specified by FilePath. If the file does not exist, it will be created.

Alias
Required False
Pipeline false
Default Value False
-NoPrefix

Do not include a Prefix

Alias
Required False
Pipeline false
Default Value False
-Encoding

Specifies the file encoding. The default is UTF8. Valid values are: -- ASCII: Uses the encoding for the ASCII (7-bit) character set. -- BigEndianUnicode: Encodes in UTF-16 format using the big-endian byte order. -- Byte: Encodes a set of characters into a sequence of bytes. -- String: Uses the encoding type for a string. -- Unicode: Encodes in UTF-16 format using the little-endian byte order. -- UTF7: Encodes in UTF-7 format. -- UTF8: Encodes in UTF-8 format. -- Unknown: The encoding type is unknown or invalid. The data can be treated as binary.

Alias
Required False
Pipeline false
Default Value UTF8
Accepted Values ASCII,BigEndianUnicode,Byte,String,Unicode,UTF7,UTF8,Unknown
-EnableException

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.

Alias
Required False
Pipeline false
Default Value False

 

Want to see the source code for this command? Check out Export-DbaServerRole on GitHub.
Want to see the Bill Of Health for this command? Check out Export-DbaServerRole.