< Back
Export-DbaDbRole
Post
NAME Export-DbaDbRole
SYNOPSIS
Exports database roles to a T-SQL file. Export includes Role creation, object permissions and Schema ownership.
SYNTAX
Export-DbaDbRole [[-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SqlCredential]
<Pscredential>] [[-InputObject] <System.Object[]>] [[-ScriptingOptionsObject]
<Microsoft.SqlServer.Management.Smo.ScriptingOptions>] [[-Database] <System.Object[]>] [[-Role] <System.Object[]>]
[[-ExcludeRole] <System.Object[]>] [-ExcludeFixedRole <Switch>] [-IncludeRoleMember <Switch>] [[-Path] <String>]
[[-FilePath] <String>] [-Passthru <Switch>] [[-BatchSeparator] <String>] [-NoClobber <Switch>] [-Append <Switch>]
[-NoPrefix <Switch>] [[-Encoding] <String>] [-EnableException <Switch>] [<CommonParameters>]
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/1 ... base-role/
PARAMETERS
-Append [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BatchSeparator [<String>]
Batch separator for scripting output. Uses the value from configuration Formatting.BatchSeparator by default.
This is normally "GO"
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<System.Object[]>]
The database(s) to process. Options for this list are auto-populated from the server. If unspecified, all
databases will be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-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
-Encoding [<String>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeFixedRole [<Switch>]
Excludes all members of fixed roles.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeRole [<System.Object[]>]
The Role(s) to exclude.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FilePath [<String>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-IncludeRoleMember [<Switch>]
Include scripting of role members in script
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-InputObject [<System.Object[]>]
Enables piping from Get-DbaDatabase and Get-DbaLogin.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoClobber [<Switch>]
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
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoPrefix [<Switch>]
Do not include a Prefix
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Passthru [<Switch>]
Output script to console only
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Path [<String>]
Specifies the directory where the file or files will be exported.
Will default to Path.DbatoolsExport Configuration entry
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Role [<System.Object[]>]
The Role(s) to process. If unspecified, all Roles will be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ScriptingOptionsObject [<Microsoft.SqlServer.Management.Smo.ScriptingOptions>]
An SMO Scripting Object that can be used to customize the output - see New-DbaScriptingOption
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. SQL Server 2005 and above supported.
Any databases in CompatibilityLevel 80 or lower will be skipped
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: Export, Role
Author: Patrick Flynn (@sqllensman)
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- 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
RELATED LINKS
https://dbatools.io/Export-DbaDbRole
SYNOPSIS
Exports database roles to a T-SQL file. Export includes Role creation, object permissions and Schema ownership.
SYNTAX
Export-DbaDbRole [[-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SqlCredential]
<Pscredential>] [[-InputObject] <System.Object[]>] [[-ScriptingOptionsObject]
<Microsoft.SqlServer.Management.Smo.ScriptingOptions>] [[-Database] <System.Object[]>] [[-Role] <System.Object[]>]
[[-ExcludeRole] <System.Object[]>] [-ExcludeFixedRole <Switch>] [-IncludeRoleMember <Switch>] [[-Path] <String>]
[[-FilePath] <String>] [-Passthru <Switch>] [[-BatchSeparator] <String>] [-NoClobber <Switch>] [-Append <Switch>]
[-NoPrefix <Switch>] [[-Encoding] <String>] [-EnableException <Switch>] [<CommonParameters>]
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/1 ... base-role/
PARAMETERS
-Append [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-BatchSeparator [<String>]
Batch separator for scripting output. Uses the value from configuration Formatting.BatchSeparator by default.
This is normally "GO"
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Database [<System.Object[]>]
The database(s) to process. Options for this list are auto-populated from the server. If unspecified, all
databases will be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-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
-Encoding [<String>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeFixedRole [<Switch>]
Excludes all members of fixed roles.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ExcludeRole [<System.Object[]>]
The Role(s) to exclude.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-FilePath [<String>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-IncludeRoleMember [<Switch>]
Include scripting of role members in script
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-InputObject [<System.Object[]>]
Enables piping from Get-DbaDatabase and Get-DbaLogin.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoClobber [<Switch>]
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
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NoPrefix [<Switch>]
Do not include a Prefix
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Passthru [<Switch>]
Output script to console only
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Path [<String>]
Specifies the directory where the file or files will be exported.
Will default to Path.DbatoolsExport Configuration entry
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Role [<System.Object[]>]
The Role(s) to process. If unspecified, all Roles will be processed.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ScriptingOptionsObject [<Microsoft.SqlServer.Management.Smo.ScriptingOptions>]
An SMO Scripting Object that can be used to customize the output - see New-DbaScriptingOption
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. SQL Server 2005 and above supported.
Any databases in CompatibilityLevel 80 or lower will be skipped
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: Export, Role
Author: Patrick Flynn (@sqllensman)
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- 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
RELATED LINKS
https://dbatools.io/Export-DbaDbRole