-
Notifications
You must be signed in to change notification settings - Fork 227
Add new commands to enable and disable SQL Server logins with comprehensive testing #2138
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 27 commits into
main
from
copilot/fix-d72cecdf-f0f0-4228-989b-b7d653c160ee
Aug 23, 2025
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
80e89e7
Initial plan
Copilot 60e4aa4
Add SQL Server login enable/disable commands with comprehensive tests
Copilot 8688166
Fix newlines and add comprehensive integration tests for login commands
Copilot 9fff622
Fix newlines and remove trailing whitespace from all files per coding…
Copilot 9fbfd20
Merge main branch with conflict resolution
Copilot 2a9fb35
Rebase PR onto latest main and enhance New-SqlDscLogin integration tests
Copilot 4569891
Fix bad instruction
johlju d8df884
Use persistent logins for integration tests and remove cleanup code
Copilot 49018b5
Remove duplicate Enable() and Disable() methods from Login class in S…
Copilot 43915e0
Replace SMO method calls with new commands in integration tests and r…
Copilot 683633e
Simplify BeforeEach blocks and add ErrorAction Stop to login commands
Copilot 919850d
Add parameter attribute validation tests, fix InModuleScope usage, ad…
Copilot 4ae5d4b
Apply suggestion from @coderabbitai[bot]
johlju 3448faf
Apply suggestion from @coderabbitai[bot]
johlju f81c4d8
Apply suggestion from @coderabbitai[bot]
johlju 4c6196b
Apply suggestion from @coderabbitai[bot]
johlju 3b97c10
Fix command OutputType and integration test improvements
Copilot a18e49c
Update CHANGELOG.md
johlju 663ea9d
Update source/Public/Disable-SqlDscLogin.ps1
johlju fa227ba
Replace OutputType with empty attribute for void commands
Copilot e839d49
Address code review feedback: improve variable naming, documentation,…
Copilot 98bc1ed
Address code review feedback: remove ValueFromPipelineByPropertyName,…
Copilot 6475ec1
Update CHANGELOG.md
johlju 5f34967
Update source/Public/Test-SqlDscIsLoginEnabled.ps1
johlju 2b486de
Update source/Public/Disable-SqlDscLogin.ps1
johlju fa44379
Update tests/Integration/Commands/Test-SqlDscIsLoginEnabled.Integrati…
johlju 8e2667f
Fix Get-SqlDscLogin mock to return single object instead of array in …
Copilot 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
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
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,120 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Disables a SQL Server login. | ||
|
|
||
| .DESCRIPTION | ||
| This command disables a SQL Server login in a SQL Server Database Engine instance. | ||
|
|
||
| .PARAMETER ServerObject | ||
| Specifies current server connection object. | ||
|
|
||
| .PARAMETER LoginObject | ||
| Specifies a login object to disable. | ||
|
|
||
| .PARAMETER Name | ||
| Specifies the name of the server login to be disabled. | ||
|
|
||
| .PARAMETER Force | ||
| Specifies that the login should be disabled without any confirmation. | ||
|
|
||
| .PARAMETER Refresh | ||
| Specifies that the **ServerObject**'s logins should be refreshed before | ||
| trying to disable the login object. This is helpful when logins could have | ||
| been modified outside of the **ServerObject**, for example through T-SQL. | ||
| But on instances with a large amount of logins it might be better to make | ||
| sure the **ServerObject** is recent enough, or pass in **LoginObject**. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $loginObject = $serverObject | Get-SqlDscLogin -Name 'MyLogin' | ||
| $loginObject | Disable-SqlDscLogin | ||
|
|
||
| Disables the login named **MyLogin**. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $serverObject | Disable-SqlDscLogin -Name 'MyLogin' | ||
|
|
||
| Disables the login named **MyLogin**. | ||
|
|
||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $serverObject | Disable-SqlDscLogin -Name 'MyLogin' -Force | ||
|
|
||
| Disables the login without confirmation using **-Force**. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $serverObject | Disable-SqlDscLogin -Name 'MyLogin' -Refresh | ||
|
|
||
| Refreshes the server logins collection before disabling **MyLogin**. | ||
| .INPUTS | ||
| [Microsoft.SqlServer.Management.Smo.Server] | ||
|
|
||
| Server object accepted from the pipeline (ServerObject parameter set). | ||
|
|
||
| [Microsoft.SqlServer.Management.Smo.Login] | ||
|
|
||
| Login object accepted from the pipeline (LoginObject parameter set). | ||
|
|
||
| .OUTPUTS | ||
| None. | ||
| #> | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| function Disable-SqlDscLogin | ||
| { | ||
| [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.')] | ||
| [OutputType()] | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] | ||
| param | ||
| ( | ||
| [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Server] | ||
| $ServerObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'LoginObject', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Login] | ||
| $LoginObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true)] | ||
| [System.String] | ||
| $Name, | ||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Force, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObject')] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Refresh | ||
| ) | ||
|
|
||
| process | ||
| { | ||
| if ($Force.IsPresent -and -not $Confirm) | ||
| { | ||
| $ConfirmPreference = 'None' | ||
| } | ||
|
|
||
| if ($PSCmdlet.ParameterSetName -eq 'ServerObject') | ||
| { | ||
| $getSqlDscLoginParameters = @{ | ||
| ServerObject = $ServerObject | ||
| Name = $Name | ||
| Refresh = $Refresh | ||
| ErrorAction = 'Stop' | ||
| } | ||
|
|
||
| # If this command does not find the login it will throw an exception. | ||
| $LoginObject = Get-SqlDscLogin @getSqlDscLoginParameters | ||
| } | ||
|
|
||
| $verboseDescriptionMessage = $script:localizedData.Login_Disable_ShouldProcessVerboseDescription -f $LoginObject.Name, $LoginObject.Parent.InstanceName | ||
| $verboseWarningMessage = $script:localizedData.Login_Disable_ShouldProcessVerboseWarning -f $LoginObject.Name | ||
| $captionMessage = $script:localizedData.Login_Disable_ShouldProcessCaption | ||
|
|
||
| if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage)) | ||
| { | ||
| $LoginObject.Disable() | ||
| } | ||
| } | ||
| } | ||
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,107 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Enables a SQL Server login. | ||
|
|
||
| .DESCRIPTION | ||
| This command enables a SQL Server login in a SQL Server Database Engine instance. | ||
|
|
||
| .PARAMETER ServerObject | ||
| Specifies current server connection object. | ||
|
|
||
| .PARAMETER LoginObject | ||
| Specifies a login object to enable. | ||
|
|
||
| .PARAMETER Name | ||
| Specifies the name of the server login to be enabled. | ||
|
|
||
| .PARAMETER Force | ||
| Specifies that the login should be enabled without any confirmation. | ||
|
|
||
| .PARAMETER Refresh | ||
| Specifies that the **ServerObject**'s logins should be refreshed before | ||
| trying to enable the login object. This is helpful when logins could have | ||
| been modified outside of the **ServerObject**, for example through T-SQL. | ||
| But on instances with a large amount of logins it might be better to make | ||
| sure the **ServerObject** is recent enough, or pass in **LoginObject**. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $loginObject = $serverObject | Get-SqlDscLogin -Name 'MyLogin' | ||
| $loginObject | Enable-SqlDscLogin | ||
|
|
||
| Enables the login named **MyLogin**. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $serverObject | Enable-SqlDscLogin -Name 'MyLogin' | ||
|
|
||
| Enables the login named **MyLogin**. | ||
|
|
||
| .INPUTS | ||
| Microsoft.SqlServer.Management.Smo.Server | ||
| When using the ServerObject parameter set, a Server object can be piped in. | ||
|
|
||
| Microsoft.SqlServer.Management.Smo.Login | ||
| When using the LoginObject parameter set, a Login object can be piped in. | ||
|
|
||
| .OUTPUTS | ||
| None. | ||
| #> | ||
| function Enable-SqlDscLogin | ||
| { | ||
| [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.')] | ||
| [OutputType()] | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| param | ||
| ( | ||
| [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Server] | ||
| $ServerObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'LoginObject', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Login] | ||
| $LoginObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true)] | ||
| [System.String] | ||
| $Name, | ||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Force, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObject')] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Refresh | ||
| ) | ||
|
|
||
| process | ||
| { | ||
| if ($Force.IsPresent -and -not $Confirm) | ||
| { | ||
| $ConfirmPreference = 'None' | ||
| } | ||
|
|
||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ($PSCmdlet.ParameterSetName -eq 'ServerObject') | ||
| { | ||
| $getSqlDscLoginParameters = @{ | ||
| ServerObject = $ServerObject | ||
| Name = $Name | ||
| Refresh = $Refresh | ||
| ErrorAction = 'Stop' | ||
| } | ||
|
|
||
| # If this command does not find the login it will throw an exception. | ||
| $LoginObject = Get-SqlDscLogin @getSqlDscLoginParameters | ||
| } | ||
|
|
||
| $verboseDescriptionMessage = $script:localizedData.Login_Enable_ShouldProcessVerboseDescription -f $LoginObject.Name, $LoginObject.Parent.InstanceName | ||
| $verboseWarningMessage = $script:localizedData.Login_Enable_ShouldProcessVerboseWarning -f $LoginObject.Name | ||
| $captionMessage = $script:localizedData.Login_Enable_ShouldProcessCaption | ||
|
|
||
| if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage)) | ||
| { | ||
| $LoginObject.Enable() | ||
| } | ||
| } | ||
| } | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
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,112 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Returns whether the server login is enabled or disabled. | ||
|
|
||
| .DESCRIPTION | ||
| Tests the state of a SQL Server login and returns a Boolean result. | ||
| When a Server object is provided, the login is resolved using | ||
| Get-SqlDscLogin (optionally refreshing the server logins first). | ||
| When a Login object is provided, its current state is evaluated directly. | ||
| .PARAMETER ServerObject | ||
| Specifies current server connection object. | ||
|
|
||
| .PARAMETER LoginObject | ||
| Specifies a login object to test. | ||
|
|
||
| .PARAMETER Name | ||
| Specifies the name of the server login to test. | ||
|
|
||
| .PARAMETER Refresh | ||
| Specifies that the **ServerObject**'s logins should be refreshed before | ||
| trying to test the login object. This is helpful when logins could have | ||
| been modified outside of the **ServerObject**, for example through T-SQL. | ||
| But on instances with a large amount of logins it might be better to make | ||
| sure the **ServerObject** is recent enough, or pass in **LoginObject**. | ||
|
|
||
| .INPUTS | ||
| [Microsoft.SqlServer.Management.Smo.Server] | ||
|
|
||
| Server object accepted from the pipeline. | ||
|
|
||
| [Microsoft.SqlServer.Management.Smo.Login] | ||
|
|
||
| Login object accepted from the pipeline. | ||
|
|
||
| .OUTPUTS | ||
| [System.Boolean] | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Returns $true if the login is enabled, $false if the login is disabled. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| Test-SqlDscIsLoginEnabled -ServerObject $serverObject -Name 'MyLogin' | ||
|
|
||
| Returns $true if the login is enabled, if not $false is returned. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $loginObject = $serverObject | Get-SqlDscLogin -Name 'MyLogin' | ||
| Test-SqlDscIsLoginEnabled -LoginObject $loginObject | ||
|
|
||
| Returns $true if the login is enabled, if not $false is returned. | ||
johlju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $result = $serverObject | Test-SqlDscIsLoginEnabled -Name 'MyLogin' | ||
|
|
||
| Demonstrates pipeline usage with ServerObject. Returns $true if the login is enabled, if not $false is returned. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $loginObject = $serverObject | Get-SqlDscLogin -Name 'MyLogin' | ||
| $result = $loginObject | Test-SqlDscIsLoginEnabled | ||
|
|
||
| Demonstrates pipeline usage with LoginObject. Returns $true if the login is enabled, if not $false is returned. | ||
| #> | ||
| function Test-SqlDscIsLoginEnabled | ||
| { | ||
| [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.')] | ||
| [CmdletBinding()] | ||
| [OutputType([System.Boolean])] | ||
| param | ||
| ( | ||
| [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Server] | ||
| $ServerObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'LoginObject', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Login] | ||
| $LoginObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true)] | ||
| [System.String] | ||
| $Name, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObject')] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Refresh | ||
| ) | ||
|
|
||
| process | ||
| { | ||
| if ($PSCmdlet.ParameterSetName -eq 'ServerObject') | ||
| { | ||
| $getSqlDscLoginParameters = @{ | ||
| ServerObject = $ServerObject | ||
| Name = $Name | ||
| Refresh = $Refresh | ||
| ErrorAction = 'Stop' | ||
| } | ||
|
|
||
| # If this command does not find the login it will throw an exception. | ||
| $loginObjectArray = Get-SqlDscLogin @getSqlDscLoginParameters | ||
|
|
||
| # Pick the only object in the array. | ||
| $LoginObject = $loginObjectArray | ||
| } | ||
|
|
||
| $loginEnabled = -not $LoginObject.IsDisabled | ||
|
|
||
| return $loginEnabled | ||
| } | ||
| } | ||
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.