-
Notifications
You must be signed in to change notification settings - Fork 227
*-SqlDscServerPermission: Add commands SQL Server permission management
#2157
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
Merged
johlju
merged 30 commits into
main
from
copilot/fix-e806ea1d-2077-4c68-a272-f8dea52701f7
Sep 1, 2025
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b41547f
Initial plan
Copilot 683ea11
Add core server permission commands and private functions
Copilot 6e04b2e
Add integration tests and update CHANGELOG for new server permission …
Copilot f4db6ab
Update server permission commands to use State parameter and ServerPe…
Copilot 4299b88
Merge branch 'main' into copilot/fix-e806ea1d-2077-4c68-a272-f8dea527…
johlju 167bac9
Restructure server permission commands based on feedback - rename com…
Copilot 24f4c67
Change Permission parameter to accept string array for easier usage
Copilot a1c5aab
Implement all requested improvements to server permission commands
Copilot 50e8487
Address feedback: update server permission commands to use Login/Serv…
Copilot 345461e
Clean up code
johlju aea4697
Fix blank line EOF
johlju 3dcc4a4
Change error handling to use PSCmdlet.ThrowTerminatingError in server…
Copilot f78e7d6
Merge branch 'main' into copilot/fix-e806ea1d-2077-4c68-a272-f8dea527…
johlju 3ec521e
Remove unused $principalObject variables from server permission commands
Copilot cb5e97a
Refactor SQL DSC Server Permission Tests
johlju 8a3dd4d
Update integration tests
johlju 2c337e3
Merge branch 'main' into copilot/fix-e806ea1d-2077-4c68-a272-f8dea527…
johlju a14e934
Remove integration tests for Get-SqlDscServerPermission
johlju 1edcd8f
Refactor permission handling in integration and unit tests to elimina…
johlju 7278bfe
Update CHANGELOG.md
johlju 5c9a575
Update tests/Integration/Commands/Deny-SqlDscServerPermission.Integra…
johlju 01d832a
Update tests/Integration/Commands/Deny-SqlDscServerPermission.Integra…
johlju eba49be
Remove unit test stub imports from server permission integration tests
Copilot bc3112f
Reorganize server permissions in SqlServerPermission enum for clarity…
johlju 8c1aaa3
Reorganize integration test commands by grouping and adding missing r…
johlju 546e22e
Refactor AfterAll block in Deny-SqlDscServerPermission integration te…
johlju a7ea287
Update Pester guidelines to consolidate requirements and best practic…
johlju 475290e
Refactor integration tests to remove default parameter values and add…
johlju 47586c2
Reorganize integration test commands to improve clarity and add missi…
johlju 360c066
Remove redundant module removal command from AfterAll blocks in integ…
johlju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| The possible server permissions that can be granted, denied, or revoked. | ||
|
|
||
| .NOTES | ||
| The available permissions can be seen in the ServerPermission Class documentation: | ||
| https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.management.smo.serverpermission | ||
| #> | ||
| enum SqlServerPermission | ||
| { | ||
| # cSpell:ignore securables | ||
| AdministerBulkOperations = 1 | ||
| AlterAnyAvailabilityGroup | ||
| AlterAnyConnection | ||
| AlterAnyCredential | ||
| AlterAnyDatabase | ||
| AlterAnyEndpoint | ||
| AlterAnyEventNotification | ||
| AlterAnyEventSession | ||
| AlterAnyEventSessionAddEvent | ||
| AlterAnyEventSessionAddTarget | ||
| AlterAnyEventSessionDisable | ||
| AlterAnyEventSessionDropEvent | ||
| AlterAnyEventSessionDropTarget | ||
| AlterAnyEventSessionEnable | ||
| AlterAnyEventSessionOption | ||
| AlterAnyLinkedServer | ||
| AlterAnyLogin | ||
| AlterAnyServerAudit | ||
| AlterAnyServerRole | ||
| AlterResources | ||
| AlterServerState | ||
| AlterSettings | ||
| AlterTrace | ||
| AuthenticateServer | ||
| ConnectAnyDatabase | ||
| ConnectSql | ||
| ControlServer | ||
| CreateAnyDatabase | ||
| CreateAnyEventSession | ||
| CreateAvailabilityGroup | ||
| CreateDdlEventNotification | ||
| CreateEndpoint | ||
| CreateLogin | ||
| CreateServerRole | ||
| CreateTraceEventNotification | ||
| DropAnyEventSession | ||
| ExternalAccessAssembly | ||
| ImpersonateAnyLogin | ||
| SelectAllUserSecurables | ||
| Shutdown | ||
| UnsafeAssembly | ||
| ViewAnyCryptographicallySecuredDefinition | ||
| ViewAnyDatabase | ||
| ViewAnyDefinition | ||
| ViewAnyErrorLog | ||
| ViewAnyPerformanceDefinition | ||
| ViewAnySecurityDefinition | ||
| ViewServerPerformanceState | ||
| ViewServerSecurityAudit | ||
| ViewServerSecurityState | ||
| ViewServerState | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Denies server permissions to a principal on a SQL Server Database Engine instance. | ||
|
|
||
| .DESCRIPTION | ||
| This command denies server permissions to an existing principal on a SQL Server | ||
| Database Engine instance. The principal can be specified as either a Login | ||
| object (from Get-SqlDscLogin) or a ServerRole object (from Get-SqlDscRole). | ||
|
|
||
| .PARAMETER Login | ||
| Specifies the Login object for which the permissions are denied. | ||
| This parameter accepts pipeline input. | ||
|
|
||
| .PARAMETER ServerRole | ||
| Specifies the ServerRole object for which the permissions are denied. | ||
| This parameter accepts pipeline input. | ||
|
|
||
| .PARAMETER Permission | ||
| Specifies the permissions to be denied. Specify multiple permissions by | ||
| providing an array of SqlServerPermission enum values. | ||
|
|
||
| .PARAMETER Force | ||
| Specifies that the permissions should be denied without any confirmation. | ||
|
|
||
| .OUTPUTS | ||
| None. | ||
|
|
||
| .EXAMPLE | ||
| $serverInstance = Connect-SqlDscDatabaseEngine | ||
| $login = $serverInstance | Get-SqlDscLogin -Name 'MyLogin' | ||
|
|
||
| Deny-SqlDscServerPermission -Login $login -Permission ConnectSql, ViewServerState | ||
|
|
||
| Denies the specified permissions to the login 'MyLogin'. | ||
|
|
||
| .EXAMPLE | ||
| $serverInstance = Connect-SqlDscDatabaseEngine | ||
| $role = $serverInstance | Get-SqlDscRole -Name 'MyRole' | ||
|
|
||
| $role | Deny-SqlDscServerPermission -Permission AlterAnyDatabase -Force | ||
|
|
||
| Denies the specified permissions to the role 'MyRole' without prompting for confirmation. | ||
|
|
||
| .NOTES | ||
| The Login or ServerRole object must come from the same SQL Server instance | ||
| where the permissions will be denied. If specifying `-ErrorAction 'SilentlyContinue'` | ||
| then the command will silently continue if any errors occur. If specifying | ||
| `-ErrorAction 'Stop'` the command will throw an error on any failure. | ||
| #> | ||
| function Deny-SqlDscServerPermission | ||
| { | ||
| [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(SupportsShouldProcess = $true, ConfirmImpact = 'High')] | ||
| [OutputType()] | ||
| param | ||
| ( | ||
| [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Login')] | ||
| [Microsoft.SqlServer.Management.Smo.Login] | ||
| $Login, | ||
|
|
||
| [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ServerRole')] | ||
| [Microsoft.SqlServer.Management.Smo.ServerRole] | ||
| $ServerRole, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [SqlServerPermission[]] | ||
| $Permission, | ||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Force | ||
| ) | ||
|
|
||
| process | ||
| { | ||
| if ($Force.IsPresent -and -not $Confirm) | ||
| { | ||
| $ConfirmPreference = 'None' | ||
| } | ||
|
|
||
| # Determine which principal object we're working with | ||
| if ($PSCmdlet.ParameterSetName -eq 'Login') | ||
| { | ||
| $principalName = $Login.Name | ||
| $serverObject = $Login.Parent | ||
| } | ||
| else | ||
| { | ||
| $principalName = $ServerRole.Name | ||
| $serverObject = $ServerRole.Parent | ||
| } | ||
|
|
||
| $verboseDescriptionMessage = $script:localizedData.ServerPermission_Deny_ShouldProcessVerboseDescription -f $principalName, $serverObject.InstanceName, ($Permission -join ',') | ||
| $verboseWarningMessage = $script:localizedData.ServerPermission_Deny_ShouldProcessVerboseWarning -f $principalName | ||
| $captionMessage = $script:localizedData.ServerPermission_Deny_ShouldProcessCaption | ||
|
|
||
| if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage)) | ||
| { | ||
| # Convert enum array to ServerPermissionSet object | ||
| $permissionSet = [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]::new() | ||
| foreach ($permissionName in $Permission) | ||
| { | ||
| $permissionSet.$permissionName = $true | ||
| } | ||
|
|
||
| # Get the permissions names that are set to $true in the ServerPermissionSet. | ||
| $permissionName = $permissionSet | | ||
| Get-Member -MemberType 'Property' | | ||
| Select-Object -ExpandProperty 'Name' | | ||
| Where-Object -FilterScript { | ||
| $permissionSet.$_ | ||
| } | ||
|
|
||
| try | ||
| { | ||
| $serverObject.Deny($permissionSet, $principalName) | ||
| } | ||
| catch | ||
| { | ||
| $errorMessage = $script:localizedData.ServerPermission_Deny_FailedToDenyPermission -f $principalName, $serverObject.InstanceName | ||
|
|
||
| $PSCmdlet.ThrowTerminatingError( | ||
| [System.Management.Automation.ErrorRecord]::new( | ||
| $errorMessage, | ||
| 'DSDSP0001', # cSpell: disable-line | ||
| [System.Management.Automation.ErrorCategory]::InvalidOperation, | ||
| $principalName | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.