|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Denies server permissions to a principal on a SQL Server Database Engine instance. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + This command denies server permissions to an existing principal on a SQL Server |
| 7 | + Database Engine instance. The principal can be specified as either a Login |
| 8 | + object (from Get-SqlDscLogin) or a ServerRole object (from Get-SqlDscRole). |
| 9 | +
|
| 10 | + .PARAMETER Login |
| 11 | + Specifies the Login object for which the permissions are denied. |
| 12 | + This parameter accepts pipeline input. |
| 13 | +
|
| 14 | + .PARAMETER ServerRole |
| 15 | + Specifies the ServerRole object for which the permissions are denied. |
| 16 | + This parameter accepts pipeline input. |
| 17 | +
|
| 18 | + .PARAMETER Permission |
| 19 | + Specifies the permissions to be denied. Specify multiple permissions by |
| 20 | + providing an array of SqlServerPermission enum values. |
| 21 | +
|
| 22 | + .PARAMETER Force |
| 23 | + Specifies that the permissions should be denied without any confirmation. |
| 24 | +
|
| 25 | + .OUTPUTS |
| 26 | + None. |
| 27 | +
|
| 28 | + .EXAMPLE |
| 29 | + $serverInstance = Connect-SqlDscDatabaseEngine |
| 30 | + $login = $serverInstance | Get-SqlDscLogin -Name 'MyLogin' |
| 31 | +
|
| 32 | + Deny-SqlDscServerPermission -Login $login -Permission ConnectSql, ViewServerState |
| 33 | +
|
| 34 | + Denies the specified permissions to the login 'MyLogin'. |
| 35 | +
|
| 36 | + .EXAMPLE |
| 37 | + $serverInstance = Connect-SqlDscDatabaseEngine |
| 38 | + $role = $serverInstance | Get-SqlDscRole -Name 'MyRole' |
| 39 | +
|
| 40 | + $role | Deny-SqlDscServerPermission -Permission AlterAnyDatabase -Force |
| 41 | +
|
| 42 | + Denies the specified permissions to the role 'MyRole' without prompting for confirmation. |
| 43 | +
|
| 44 | + .NOTES |
| 45 | + The Login or ServerRole object must come from the same SQL Server instance |
| 46 | + where the permissions will be denied. If specifying `-ErrorAction 'SilentlyContinue'` |
| 47 | + then the command will silently continue if any errors occur. If specifying |
| 48 | + `-ErrorAction 'Stop'` the command will throw an error on any failure. |
| 49 | +#> |
| 50 | +function Deny-SqlDscServerPermission |
| 51 | +{ |
| 52 | + [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.')] |
| 53 | + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('AvoidThrowOutsideOfTry', '', Justification = 'Because the code throws based on an prior expression')] |
| 54 | + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] |
| 55 | + [OutputType()] |
| 56 | + param |
| 57 | + ( |
| 58 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Login')] |
| 59 | + [Microsoft.SqlServer.Management.Smo.Login] |
| 60 | + $Login, |
| 61 | + |
| 62 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ServerRole')] |
| 63 | + [Microsoft.SqlServer.Management.Smo.ServerRole] |
| 64 | + $ServerRole, |
| 65 | + |
| 66 | + [Parameter(Mandatory = $true)] |
| 67 | + [SqlServerPermission[]] |
| 68 | + $Permission, |
| 69 | + |
| 70 | + [Parameter()] |
| 71 | + [System.Management.Automation.SwitchParameter] |
| 72 | + $Force |
| 73 | + ) |
| 74 | + |
| 75 | + process |
| 76 | + { |
| 77 | + if ($Force.IsPresent -and -not $Confirm) |
| 78 | + { |
| 79 | + $ConfirmPreference = 'None' |
| 80 | + } |
| 81 | + |
| 82 | + # Determine which principal object we're working with |
| 83 | + if ($PSCmdlet.ParameterSetName -eq 'Login') |
| 84 | + { |
| 85 | + $principalName = $Login.Name |
| 86 | + $serverObject = $Login.Parent |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + $principalName = $ServerRole.Name |
| 91 | + $serverObject = $ServerRole.Parent |
| 92 | + } |
| 93 | + |
| 94 | + $verboseDescriptionMessage = $script:localizedData.ServerPermission_Deny_ShouldProcessVerboseDescription -f $principalName, $serverObject.InstanceName, ($Permission -join ',') |
| 95 | + $verboseWarningMessage = $script:localizedData.ServerPermission_Deny_ShouldProcessVerboseWarning -f $principalName |
| 96 | + $captionMessage = $script:localizedData.ServerPermission_Deny_ShouldProcessCaption |
| 97 | + |
| 98 | + if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage)) |
| 99 | + { |
| 100 | + # Convert enum array to ServerPermissionSet object |
| 101 | + $permissionSet = [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]::new() |
| 102 | + foreach ($permissionName in $Permission) |
| 103 | + { |
| 104 | + $permissionSet.$permissionName = $true |
| 105 | + } |
| 106 | + |
| 107 | + # Get the permissions names that are set to $true in the ServerPermissionSet. |
| 108 | + $permissionName = $permissionSet | |
| 109 | + Get-Member -MemberType 'Property' | |
| 110 | + Select-Object -ExpandProperty 'Name' | |
| 111 | + Where-Object -FilterScript { |
| 112 | + $permissionSet.$_ |
| 113 | + } |
| 114 | + |
| 115 | + try |
| 116 | + { |
| 117 | + $serverObject.Deny($permissionSet, $principalName) |
| 118 | + } |
| 119 | + catch |
| 120 | + { |
| 121 | + $errorMessage = $script:localizedData.ServerPermission_Deny_FailedToDenyPermission -f $principalName, $serverObject.InstanceName |
| 122 | + |
| 123 | + $PSCmdlet.ThrowTerminatingError( |
| 124 | + [System.Management.Automation.ErrorRecord]::new( |
| 125 | + $errorMessage, |
| 126 | + 'DSDSP0001', # cSpell: disable-line |
| 127 | + [System.Management.Automation.ErrorCategory]::InvalidOperation, |
| 128 | + $principalName |
| 129 | + ) |
| 130 | + ) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments