-
Notifications
You must be signed in to change notification settings - Fork 226
Get-SqlDscLogin: command to retrieve SQL Server logins
#2134
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
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ea52565
`Get-SqlDscLogin`: function to retrieve SQL Server logins
johlju e382411
Force import of SqlServerDsc module in test setup
johlju 66a4d34
Remove unnecessary stub cmdlet loading and cleanup from Get-SqlDscLog…
johlju 15d6926
Fix connection
johlju b713731
Rename error message for missing login in Get-SqlDscLogin to improve …
johlju ccd0a7f
Remove reference to passing **LoginObject** in Get-SqlDscLogin docume…
johlju 8f3d576
Remove unnecessary suppression of PSUseOutputTypeCorrectly warning in…
johlju da84289
Fix array access syntax in Get-SqlDscLogin integration tests for type…
johlju a21c6d2
Fix expected error message format in Get-SqlDscLogin integration test…
johlju 452005a
Fix array count comparison in Get-SqlDscLogin integration tests to en…
johlju 12614f0
Refactor Get-SqlDscLogin integration tests to use script-scoped varia…
johlju 4519969
Update documentation for Get-SqlDscLogin to clarify output types and …
johlju 45a8000
Fix comments for clarity and consistency in Get-SqlDscLogin integrati…
johlju 33dc0e2
Fix comments for clarity and correct grammatical errors in Get-SqlDsc…
johlju 72d1c69
Change error category from 'InvalidOperation' to 'ObjectNotFound' in …
johlju 1c03af1
Add tests for parameter metadata in Get-SqlDscLogin function
johlju 22e3e7a
Update source/Public/Get-SqlDscLogin.ps1
johlju 7606c11
Update source/Public/Get-SqlDscLogin.ps1
johlju 3b68e4d
Update source/Public/Get-SqlDscLogin.ps1
johlju 7c31fd9
Update source/Public/Get-SqlDscLogin.ps1
johlju 7bad8de
Add verbose logging for login retrieval in Get-SqlDscLogin function
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Gets SQL Server logins. | ||
|
|
||
| .DESCRIPTION | ||
| Retrieves login objects from a SQL Server Database Engine instance. Specify -Name | ||
| to return a specific login, or omit -Name to return all logins. Use -Refresh to | ||
| refresh the login collection before retrieval. | ||
|
|
||
| .PARAMETER ServerObject | ||
| Specifies the current server connection object. | ||
| .PARAMETER Name | ||
| Specifies the name of the server login to get. | ||
|
|
||
| .PARAMETER Refresh | ||
| Specifies that the **ServerObject** logins should be refreshed before | ||
| trying to get the login object. This is helpful when logins might have | ||
| been modified outside of the **ServerObject**, for example through T-SQL. | ||
| On instances with a large number of logins, consider ensuring the | ||
| **ServerObject** is recent enough. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $serverObject | Get-SqlDscLogin -Name 'MyLogin' | ||
|
|
||
| Get the login named **MyLogin**. | ||
|
|
||
| .OUTPUTS | ||
| `[Microsoft.SqlServer.Management.Smo.Login]` | ||
|
|
||
| Returns a single Login object when the Name parameter is specified and a | ||
| match is found. | ||
|
|
||
| .OUTPUTS | ||
| `[Microsoft.SqlServer.Management.Smo.Login[]]` | ||
|
|
||
| Returns an array of Login objects when the Name parameter is not specified | ||
| (returns all logins) or when multiple matches are found. | ||
| #> | ||
|
johlju marked this conversation as resolved.
|
||
| function Get-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([Microsoft.SqlServer.Management.Smo.Login[]])] | ||
| [CmdletBinding()] | ||
| param | ||
| ( | ||
| [Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Server] | ||
| $ServerObject, | ||
|
|
||
| [Parameter()] | ||
| [System.String] | ||
| $Name, | ||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Refresh | ||
| ) | ||
|
|
||
| process | ||
| { | ||
| if ($Refresh.IsPresent) | ||
| { | ||
| Write-Verbose -Message ($script:localizedData.Login_Get_RefreshingLogins -f $ServerObject.InstanceName) | ||
|
|
||
| # Make sure the logins are up-to-date to get any newly created logins. | ||
| $ServerObject.Logins.Refresh() | ||
| } | ||
|
|
||
| $loginObject = @() | ||
|
|
||
| if ($PSBoundParameters.ContainsKey('Name')) | ||
| { | ||
| Write-Verbose -Message ($script:localizedData.Login_Get_RetrievingByName -f $Name, $ServerObject.InstanceName) | ||
|
|
||
| $loginObject = $ServerObject.Logins[$Name] | ||
|
|
||
| if (-not $loginObject) | ||
| { | ||
| $missingLoginMessage = $script:localizedData.Login_Get_Missing -f $Name | ||
|
|
||
| $writeErrorParameters = @{ | ||
| Message = $missingLoginMessage | ||
| Category = 'ObjectNotFound' | ||
| ErrorId = 'GSDL0001' # cspell: disable-line | ||
| TargetObject = $Name | ||
| } | ||
|
|
||
| Write-Error @writeErrorParameters | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Write-Verbose -Message ($script:localizedData.Login_Get_ReturningAllLogins -f $ServerObject.InstanceName) | ||
|
|
||
| $loginObject = $ServerObject.Logins | ||
| } | ||
|
|
||
| return [Microsoft.SqlServer.Management.Smo.Login[]] $loginObject | ||
| } | ||
| } | ||
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
111 changes: 111 additions & 0 deletions
111
tests/Integration/Commands/Get-SqlDscLogin.Integration.Tests.ps1
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,111 @@ | ||
| [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] | ||
| param () | ||
|
|
||
| BeforeDiscovery { | ||
| try | ||
| { | ||
| if (-not (Get-Module -Name 'DscResource.Test')) | ||
| { | ||
| # Assumes dependencies have been resolved, so if this module is not available, run 'noop' task. | ||
| if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) | ||
| { | ||
| # Redirect all streams to $null, except the error stream (stream 2) | ||
| & "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null | ||
| } | ||
|
|
||
| # If the dependencies have not been resolved, this will throw an error. | ||
| Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' | ||
| } | ||
| } | ||
| catch [System.IO.FileNotFoundException] | ||
| { | ||
| throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' | ||
| } | ||
| } | ||
|
|
||
| BeforeAll { | ||
| $script:dscModuleName = 'SqlServerDsc' | ||
|
|
||
| Import-Module -Name $script:dscModuleName | ||
| } | ||
|
|
||
| Describe 'Get-SqlDscLogin' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { | ||
| BeforeAll { | ||
| # Starting the named instance SQL Server service prior to running tests. | ||
| Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop' | ||
|
|
||
| $script:mockInstanceName = 'DSCSQLTEST' | ||
| $script:mockComputerName = Get-ComputerName | ||
|
|
||
| $mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. | ||
|
johlju marked this conversation as resolved.
|
||
| $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force | ||
|
|
||
| $script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword) | ||
|
|
||
| $script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential | ||
| } | ||
|
|
||
| AfterAll { | ||
| # Stop the named instance SQL Server service to save memory on the build worker. | ||
| Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop' | ||
| } | ||
|
|
||
|
|
||
| Context 'When getting all SQL Server logins' { | ||
| It 'Should return an array of Login objects' { | ||
| $result = Get-SqlDscLogin -ServerObject $script:serverObject | ||
|
|
||
| <# | ||
| Casting to array to ensure we get the count on Windows PowerShell | ||
| when there is only one login. | ||
| #> | ||
| @($result).Count | Should -BeGreaterOrEqual 1 | ||
| @($result)[0] | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' | ||
| } | ||
|
|
||
| It 'Should return system logins including sa' { | ||
| $result = Get-SqlDscLogin -ServerObject $script:serverObject | ||
|
|
||
| $result.Name | Should -Contain 'sa' | ||
| } | ||
| } | ||
|
|
||
| Context 'When getting a specific SQL Server login' { | ||
| It 'Should return the specified login when it exists' { | ||
| $result = Get-SqlDscLogin -ServerObject $script:serverObject -Name 'sa' | ||
|
|
||
| $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' | ||
| $result.Name | Should -Be 'sa' | ||
| $result.LoginType | Should -Be 'SqlLogin' | ||
| } | ||
|
|
||
| It 'Should throw an error when the login does not exist' { | ||
| { Get-SqlDscLogin -ServerObject $script:serverObject -Name 'NonExistentLogin' -ErrorAction 'Stop' } | | ||
| Should -Throw -ExpectedMessage 'There is no login with the name ''NonExistentLogin''.' | ||
| } | ||
|
|
||
| It 'Should return null when the login does not exist and error action is SilentlyContinue' { | ||
| $result = Get-SqlDscLogin -ServerObject $script:serverObject -Name 'NonExistentLogin' -ErrorAction 'SilentlyContinue' | ||
|
|
||
| $result | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
|
|
||
| Context 'When using the Refresh parameter' { | ||
| It 'Should return the same results with and without Refresh' { | ||
| $resultWithoutRefresh = Get-SqlDscLogin -ServerObject $script:serverObject | ||
| $resultWithRefresh = Get-SqlDscLogin -ServerObject $script:serverObject -Refresh | ||
|
|
||
| @($resultWithoutRefresh).Count | Should -Be @($resultWithRefresh).Count | ||
| } | ||
| } | ||
|
|
||
| Context 'When using pipeline input' { | ||
| It 'Should accept ServerObject from pipeline' { | ||
| $result = $script:serverObject | Get-SqlDscLogin -Name 'sa' | ||
|
|
||
| $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' | ||
| $result.Name | Should -Be 'sa' | ||
| } | ||
| } | ||
| } | ||
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.