commands

^

Export-DbaDbRole

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

 

Synopsis

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

Description

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

This command is based off 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-DbaDbRole
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-InputObject] <Object[]>]
    [[-ScriptingOptionsObject] <ScriptingOptions>]
    [[-Database] <Object[]>]
    [[-Role] <Object[]>]
    [[-ExcludeRole] <Object[]>]
    [-ExcludeFixedRole]
    [-IncludeRoleMember]
    [[-Path] <String>]
    [[-FilePath] <String>]
    [-Passthru]
    [[-BatchSeparator] <String>]
    [-NoClobber]
    [-Append]
    [-NoPrefix]
    [[-Encoding] <String>]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Export-DbaDbRole -SqlInstance sql2005 -Path C:\temp

Exports all the Database Roles for SQL Server "sql2005" and writes them to the file "C:\temp\sql2005-logins.sql"

Example: 2
PS C:\> Export-DbaDbRole -SqlInstance sqlserver2014a -ExcludeRole realcajun -SqlCredential $scred -Path C:\temp\roles.sql -Append

Authenticates to sqlserver2014a using SQL Authentication. Exports all roles except for realcajun to C:\temp\roles.sql, and appends to the file if it exists. If not, the file will be created.

Example: 3
PS C:\> Export-DbaDbRole -SqlInstance sqlserver2014a -Role realcajun,netnerds -Path C:\temp\roles.sql

Exports ONLY roles netnerds and realcajun FROM sqlserver2014a to the file C:\temp\roles.sql

Example: 4
PS C:\> Export-DbaDbRole -SqlInstance sqlserver2014a -Role realcajun,netnerds -Database HR, Accounting

Exports ONLY roles netnerds and realcajun FROM sqlserver2014a with the permissions on databases HR and Accounting

Example: 5
PS C:\> Get-DbaDatabase -SqlInstance sqlserver2014a -Database HR, Accounting | Export-DbaDbRole

Exports ONLY roles FROM sqlserver2014a with permissions on databases HR and Accounting

Example: 6
PS C:\> Set-DbatoolsConfig -FullName formatting.batchseparator -Value $null
PS C:\> Export-DbaDbRole -SqlInstance sqlserver2008 -Role realcajun,netnerds -Path C:\temp\roles.sql

Sets the BatchSeparator configuration to null, removing the default "GO" value.
Exports ONLY roles netnerds and realcajun FROM sqlserver2008 server, to the C:\temp\roles.sql file, without the "GO" batch separator.

Example: 7
PS C:\> Export-DbaDbRole -SqlInstance sqlserver2008 -Role realcajun,netnerds -Path C:\temp\roles.sql -BatchSeparator $null

Exports ONLY roles netnerds and realcajun FROM sqlserver2008 server, to the C:\temp\roles.sql file, without the "GO" batch separator.

Example: 8
PS C:\> Get-DbaDatabase -SqlInstance sqlserver2008 | Export-DbaDbRole -Role realcajun

Exports role realcajun for all databases on sqlserver2008

Example: 9
PS C:\> Get-DbaDbRole -SqlInstance sqlserver2008 -ExcludeFixedRole | Export-DbaDbRole

Exports all roles from all databases on sqlserver2008, excludes all roles marked as as FixedRole

Optional Parameters

-SqlInstance

The target SQL Server instance or instances. SQL Server 2005 and above supported. Any databases in CompatibilityLevel 80 or lower will be skipped

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-DbaDatabase and Get-DbaLogin.

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
-Database

The database(s) to process. Options for this list are auto-populated from the server. If unspecified, all databases will be processed.

Alias
Required False
Pipeline false
Default Value
-Role

The Role(s) to process. If unspecified, all Roles will be processed.

Alias
Required False
Pipeline false
Default Value
-ExcludeRole

The Role(s) to exclude.

Alias
Required False
Pipeline false
Default Value
-ExcludeFixedRole

Excludes all members of fixed roles.

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-DbaDbRole on GitHub.
Want to see the Bill Of Health for this command? Check out Export-DbaDbRole.