Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export-DbaServerRole: Fix case in sql to fix issues on case sensitive instances #9271

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions public/Export-DbaServerRole.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,38 @@ function Export-DbaServerRole {
$commandName = $MyInvocation.MyCommand.Name

$roleSQL = "SELECT
CASE SPerm.state
CASE sperm.state
WHEN 'D' THEN 'DENY'
WHEN 'G' THEN 'GRANT'
WHEN 'R' THEN 'REVOKE'
WHEN 'W' THEN 'GRANT'
END as GrantState,
sPerm.permission_name as Permission,
sperm.permission_name as Permission,
Case
WHEN SPerm.class = 100 THEN ''
WHEN SPerm.class = 101 AND sp2.type = 'S' THEN 'ON LOGIN::' + QuoteName(sp2.name)
WHEN SPerm.class = 101 AND sp2.type = 'R' THEN 'ON SERVER ROLE::' + QuoteName(sp2.name)
WHEN SPerm.class = 101 AND sp2.type = 'U' THEN 'ON LOGIN::' + QuoteName(sp2.name)
WHEN SPerm.class = 105 THEN 'ON ENDPOINT::' + QuoteName(ep.name)
WHEN SPerm.class = 108 THEN 'ON AVAILABILITY GROUP::' + QUOTENAME(ag.name)
WHEN sperm.class = 100 THEN ''
WHEN sperm.class = 101 AND sp2.type = 'S' THEN 'ON LOGIN::' + QuoteName(sp2.name)
WHEN sperm.class = 101 AND sp2.type = 'R' THEN 'ON SERVER ROLE::' + QuoteName(sp2.name)
WHEN sperm.class = 101 AND sp2.type = 'U' THEN 'ON LOGIN::' + QuoteName(sp2.name)
WHEN sperm.class = 105 THEN 'ON ENDPOINT::' + QuoteName(ep.name)
WHEN sperm.class = 108 THEN 'ON AVAILABILITY GROUP::' + QUOTENAME(ag.name)
ELSE ''
END as OnClause,
QuoteName(SP.name) as RoleName,
QuoteName(sp.name) as RoleName,
Case
WHEN SPerm.state = 'W' THEN 'WITH GRANT OPTION AS ' + QUOTENAME(gsp.Name)
WHEN sperm.state = 'W' THEN 'WITH GRANT OPTION AS ' + QUOTENAME(gsp.Name)
ELSE ''
END as GrantOption
FROM sys.server_permissions SPerm
INNER JOIN sys.server_principals SP
ON SP.principal_id = SPerm.grantee_principal_id
FROM sys.server_permissions sperm
INNER JOIN sys.server_principals sp
ON sp.principal_id = sperm.grantee_principal_id
INNER JOIN sys.server_principals gsp
ON gsp.principal_id = SPerm.grantor_principal_id
ON gsp.principal_id = sperm.grantor_principal_id
LEFT JOIN sys.endpoints ep
ON ep.endpoint_id = SPerm.major_id
AND SPerm.class = 105
ON ep.endpoint_id = sperm.major_id
AND sperm.class = 105
LEFT JOIN sys.server_principals sp2
ON sp2.principal_id = SPerm.major_id
AND SPerm.class = 101
ON sp2.principal_id = sperm.major_id
AND sperm.class = 101
LEFT JOIN
(
Select
Expand All @@ -205,8 +205,8 @@ function Export-DbaServerRole {
INNER JOIN sys.availability_replicas ar
ON ag.group_id = ar.group_id
) ag
ON ag.replica_metadata_id = SPerm.major_id
AND SPerm.class = 108
ON ag.replica_metadata_id = sperm.major_id
AND sperm.class = 108
where sp.type='R'
and sp.name=N'/*RoleName*/'"

Expand Down
Loading