-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathGet-SqlDscDatabasePermission.ps1
More file actions
125 lines (100 loc) · 4.84 KB
/
Get-SqlDscDatabasePermission.ps1
File metadata and controls
125 lines (100 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<#
.SYNOPSIS
Returns the current permissions for the database principal.
.DESCRIPTION
Returns the current permissions for the database principal.
.PARAMETER ServerObject
Specifies current server connection object.
.PARAMETER DatabaseName
Specifies the database name.
.PARAMETER Name
Specifies the name of the database principal for which the permissions are
returned.
.PARAMETER Refresh
Specifies that the database's principal collections (Users, Roles, and
ApplicationRoles) should be refreshed before testing if the principal exists.
This is helpful when principals could have been modified outside of the
**ServerObject**, for example through T-SQL. But on databases with a large
amount of principals it might be better to make sure the **ServerObject**
is recent enough.
.OUTPUTS
[Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]]
.EXAMPLE
$serverInstance = Connect-SqlDscDatabaseEngine
Get-SqlDscDatabasePermission -ServerObject $serverInstance -DatabaseName 'MyDatabase' -Name 'MyPrincipal'
Get the permissions for the principal 'MyPrincipal'.
.EXAMPLE
$serverInstance = Connect-SqlDscDatabaseEngine
Get-SqlDscDatabasePermission -ServerObject $serverInstance -DatabaseName 'MyDatabase' -Name 'MyPrincipal' -Refresh
Get the permissions for the principal 'MyPrincipal'. The database's principal
collections are refreshed before testing if the principal exists.
.NOTES
This command excludes fixed roles like _db_datareader_ by default, and will
always return `$null` if a fixed role is specified as **Name**.
If specifying `-ErrorAction 'SilentlyContinue'` then the command will silently
ignore if the database (parameter **DatabaseName**) is not present or the
database principal is not present. In such case the command will return `$null`.
If specifying `-ErrorAction 'Stop'` the command will throw an error if the
database or database principal is missing.
#>
function Get-SqlDscDatabasePermission
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('AvoidThrowOutsideOfTry', '', Justification = 'Because the code throws based on an prior expression')]
[CmdletBinding()]
[OutputType([Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]])]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.SqlServer.Management.Smo.Server]
$ServerObject,
[Parameter(Mandatory = $true)]
[System.String]
$DatabaseName,
[Parameter(Mandatory = $true)]
[System.String]
$Name,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Refresh
)
# cSpell: ignore GSDDP
process
{
$getSqlDscDatabasePermissionResult = $null
$sqlDatabaseObject = $null
if ($ServerObject.Databases)
{
$sqlDatabaseObject = $ServerObject.Databases[$DatabaseName]
}
if ($sqlDatabaseObject)
{
$testSqlDscIsDatabasePrincipalParameters = @{
ServerObject = $ServerObject
DatabaseName = $DatabaseName
Name = $Name
ExcludeFixedRoles = $true
}
if ($Refresh.IsPresent)
{
$testSqlDscIsDatabasePrincipalParameters.Refresh = $true
}
$isDatabasePrincipal = Test-SqlDscIsDatabasePrincipal @testSqlDscIsDatabasePrincipalParameters
if ($isDatabasePrincipal)
{
$getSqlDscDatabasePermissionResult = $sqlDatabaseObject.EnumDatabasePermissions($Name)
}
else
{
$missingPrincipalMessage = $script:localizedData.DatabasePermission_MissingPrincipal -f $Name, $DatabaseName
Write-Error -Message $missingPrincipalMessage -Category 'InvalidOperation' -ErrorId 'GSDDP0001' -TargetObject $Name
}
}
else
{
$missingDatabaseMessage = $script:localizedData.DatabasePermission_MissingDatabase -f $DatabaseName
Write-Error -Message $missingDatabaseMessage -Category 'InvalidOperation' -ErrorId 'GSDDP0002' -TargetObject $DatabaseName
}
return [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] $getSqlDscDatabasePermissionResult
}
}