diff --git a/CHANGELOG.md b/CHANGELOG.md index fdae13468..c967d7918 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Connect-SqlDscDatabaseEngine` - Added integration test for the command. +### Changed + +- `Connect-SqlDscDatabaseEngine` + - Update comment-based help with more examples. + ### Fixed - `Connect-SqlDscDatabaseEngine` diff --git a/source/Public/Connect-SqlDscDatabaseEngine.ps1 b/source/Public/Connect-SqlDscDatabaseEngine.ps1 index 53fc2d0b0..700847ca8 100644 --- a/source/Public/Connect-SqlDscDatabaseEngine.ps1 +++ b/source/Public/Connect-SqlDscDatabaseEngine.ps1 @@ -49,6 +49,16 @@ Connects to the instance 'MyInstance' on the server 'sql.company.local'. + .EXAMPLE + Connect-SqlDscDatabaseEngine -Credential ([System.Management.Automation.PSCredential]::new('DOMAIN\SqlUser', (ConvertTo-SecureString -String 'MyP@ssw0rd1' -AsPlainText -Force))) + + Connects to the default instance on the local server impersonating the Windows user 'DOMAIN\SqlUser'. + + .EXAMPLE + Connect-SqlDscDatabaseEngine -LoginType 'SqlLogin' -Credential ([System.Management.Automation.PSCredential]::new('sa', (ConvertTo-SecureString -String 'MyP@ssw0rd1' -AsPlainText -Force))) + + Connects to the default instance on the local server using the SQL login 'sa'. + .OUTPUTS `[Microsoft.SqlServer.Management.Smo.Server]` #> diff --git a/tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1 b/tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1 index 223a517a4..44141ab4f 100644 --- a/tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1 +++ b/tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1 @@ -52,6 +52,49 @@ Describe 'Connect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2016', 'Integrati } + $sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters + + $sqlServerObject.Status.ToString() | Should -Match '^Online$' + } | Should -Not -Throw + } + } + + Context 'When connecting to the named instance impersonating a Windows user' { + It 'Should return the correct result' { + { + $sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. + $sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force + + $connectSqlDscDatabaseEngineParameters = @{ + InstanceName = 'DSCSQLTEST' + Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword) + Verbose = $true + ErrorAction = 'Stop' + } + + + $sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters + + $sqlServerObject.Status.ToString() | Should -Match '^Online$' + } | Should -Not -Throw + } + } + + Context 'When connecting to the named instance using a SQL login' { + It 'Should return the correct result' { + { + $sqlAdministratorUserName = 'sa' + $sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force + + $connectSqlDscDatabaseEngineParameters = @{ + InstanceName = 'DSCSQLTEST' # cSpell: disable-line + LoginType = 'SqlLogin' + Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword) + Verbose = $true + ErrorAction = 'Stop' + } + + $sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters $sqlServerObject.Status.ToString() | Should -Match '^Online$' diff --git a/tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1 b/tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1 index 6668f40ed..846785b10 100644 --- a/tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1 +++ b/tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1 @@ -26,14 +26,14 @@ BeforeDiscovery { Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { BeforeAll { Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose + + $computerName = Get-ComputerName } Context 'When using Install parameter set' { Context 'When installing database engine default instance' { It 'Should run the command without throwing' { { - $computerName = Get-ComputerName - # Set splatting parameters for Install-SqlDscServer $installSqlDscServerParameters = @{ Install = $true @@ -158,95 +158,130 @@ Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL20 # $modulePath = Split-Path -Parent -Path (Get-Module -name SqlServerDsc -ListAvailable).ModuleBase # } - # It 'Should run the command without throwing' { - # { - # <# - # Fails with the following error message: - - # VERBOSE: Exit code (Decimal): -2068774911 - # VERBOSE: Exit facility code: 1201 - # VERBOSE: Exit error code: 1 - # VERBOSE: Exit message: There was an error generating the XML document. - - # Searches points to a permission issue, but the user has been - # granted the local administrator permissions. But code be - # Searches also points to user right SeEnableDelegationPrivilege - # which was not evaluated if it was set correctly or even needed. - # #> - # $installScriptBlock = { - # param - # ( - # [Parameter(Mandatory = $true)] - # [System.String] - # $IsoDrivePath, - - # [Parameter(Mandatory = $true)] - # [System.String] - # $ComputerName, - - # [Parameter(Mandatory = $true)] - # [System.String] - # $ModulePath - # ) - - # Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose - - # Import-Module -Name $ModulePath -Force -ErrorAction 'Stop' - - # # Set splatting parameters for Install-SqlDscServer - # $installSqlDscServerParameters = @{ - # Install = $true - # AcceptLicensingTerms = $true - # InstanceName = 'DSCSQLTEST' # cSpell: disable-line - # Features = 'SQLENGINE' - # SqlSysAdminAccounts = @( - # ('{0}\SqlAdmin' -f $ComputerName) - # ) - # SqlSvcAccount = '{0}\svc-SqlPrimary' -f $ComputerName - # SqlSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force - # SqlSvcStartupType = 'Automatic' - # AgtSvcAccount = '{0}\svc-SqlAgentPri' -f $ComputerName - # AgtSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force - # AgtSvcStartupType = 'Automatic' - # BrowserSvcStartupType = 'Automatic' - # SecurityMode = 'SQL' - # SAPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force - # SqlCollation = 'Finnish_Swedish_CI_AS' - # InstallSharedDir = 'C:\Program Files\Microsoft SQL Server' - # InstallSharedWOWDir = 'C:\Program Files (x86)\Microsoft SQL Server' - # NpEnabled = $true - # TcpEnabled = $true - # MediaPath = $IsoDrivePath - # Verbose = $true - # ErrorAction = 'Stop' - # Force = $true - # } - - # Install-SqlDscServer @installSqlDscServerParameters - # } - - # $invokeCommandUsername = 'SqlInstall' # Using computer name as NetBIOS name throw exception. - # $invokeCommandPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force - # $invokeCommandCredential = New-Object System.Management.Automation.PSCredential ($invokeCommandUsername, $invokeCommandPassword) - - # # Runs command as SqlInstall user. - # Invoke-Command -ComputerName 'localhost' -Credential $invokeCommandCredential -ScriptBlock $installScriptBlock -ArgumentList @( - # $env:IsoDrivePath, # Already set by the prerequisites tests - # (Get-ComputerName), - # $modulePath - # ) - # } | Should -Not -Throw - # } + It 'Should run the command without throwing' { + { + # Set splatting parameters for Install-SqlDscServer + $installSqlDscServerParameters = @{ + Install = $true + AcceptLicensingTerms = $true + InstanceName = 'DSCSQLTEST' + Features = 'SQLENGINE' + SqlSysAdminAccounts = @( + ('{0}\SqlAdmin' -f $computerName) + ) + SqlSvcAccount = '{0}\svc-SqlPrimary' -f $computerName + SqlSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force + SqlSvcStartupType = 'Automatic' + AgtSvcAccount = '{0}\svc-SqlAgentPri' -f $computerName + AgtSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force + AgtSvcStartupType = 'Automatic' + BrowserSvcStartupType = 'Automatic' + SecurityMode = 'SQL' + SAPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force + SqlCollation = 'Finnish_Swedish_CI_AS' + InstallSharedDir = 'C:\Program Files\Microsoft SQL Server' + InstallSharedWOWDir = 'C:\Program Files (x86)\Microsoft SQL Server' + NpEnabled = $true + TcpEnabled = $true + MediaPath = $env:IsoDrivePath + Verbose = $true + ErrorAction = 'Stop' + Force = $true + } - # It 'Should have installed the SQL Server database engine' { - # # Validate the SQL Server installation - # $sqlServerService = Get-Service -Name 'SQL Server (DSCSQLTEST)' # cSpell: disable-line + Install-SqlDscServer @installSqlDscServerParameters - # $sqlServerService | Should -Not -BeNullOrEmpty - # $sqlServerService.Status | Should -Be 'Running' - # } + # { + # <# + # Fails with the following error message: + + # VERBOSE: Exit code (Decimal): -2068774911 + # VERBOSE: Exit facility code: 1201 + # VERBOSE: Exit error code: 1 + # VERBOSE: Exit message: There was an error generating the XML document. + + # Searches points to a permission issue, but the user has been + # granted the local administrator permissions. But code be + # Searches also points to user right SeEnableDelegationPrivilege + # which was not evaluated if it was set correctly or even needed. + # #> + # $installScriptBlock = { + # param + # ( + # [Parameter(Mandatory = $true)] + # [System.String] + # $IsoDrivePath, + + # [Parameter(Mandatory = $true)] + # [System.String] + # $ComputerName, + + # [Parameter(Mandatory = $true)] + # [System.String] + # $ModulePath + # ) + + # Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose + + # Import-Module -Name $ModulePath -Force -ErrorAction 'Stop' + + # # Set splatting parameters for Install-SqlDscServer + # $installSqlDscServerParameters = @{ + # Install = $true + # AcceptLicensingTerms = $true + # InstanceName = 'DSCSQLTEST' # cSpell: disable-line + # Features = 'SQLENGINE' + # SqlSysAdminAccounts = @( + # ('{0}\SqlAdmin' -f $ComputerName) + # ) + # SqlSvcAccount = '{0}\svc-SqlPrimary' -f $ComputerName + # SqlSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force + # SqlSvcStartupType = 'Automatic' + # AgtSvcAccount = '{0}\svc-SqlAgentPri' -f $ComputerName + # AgtSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force + # AgtSvcStartupType = 'Automatic' + # BrowserSvcStartupType = 'Automatic' + # SecurityMode = 'SQL' + # SAPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force + # SqlCollation = 'Finnish_Swedish_CI_AS' + # InstallSharedDir = 'C:\Program Files\Microsoft SQL Server' + # InstallSharedWOWDir = 'C:\Program Files (x86)\Microsoft SQL Server' + # NpEnabled = $true + # TcpEnabled = $true + # MediaPath = $IsoDrivePath + # Verbose = $true + # ErrorAction = 'Stop' + # Force = $true + # } + + # Install-SqlDscServer @installSqlDscServerParameters + # } + + # $invokeCommandUsername = 'SqlInstall' # Using computer name as NetBIOS name throw exception. + # $invokeCommandPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force + # $invokeCommandCredential = New-Object System.Management.Automation.PSCredential ($invokeCommandUsername, $invokeCommandPassword) + + # # Runs command as SqlInstall user. + # Invoke-Command -ComputerName 'localhost' -Credential $invokeCommandCredential -ScriptBlock $installScriptBlock -ArgumentList @( + # $env:IsoDrivePath, # Already set by the prerequisites tests + # (Get-ComputerName), + # $modulePath + # ) + # } | Should -Not -Throw + } | Should -Not -Throw + } + + It 'Should have installed the SQL Server database engine' { + # Validate the SQL Server installation + $sqlServerService = Get-Service -Name 'SQL Server (DSCSQLTEST)' # cSpell: disable-line + + $sqlServerService | Should -Not -BeNullOrEmpty + $sqlServerService.Status | Should -Be 'Running' + } } + # # Enable this to debugging the last installation by output the Summary.txt. + # # Currently there seems impossible to run this only when an It-block fails. # Context 'Output the Summary.txt log file' { # BeforeAll { # <# diff --git a/tests/Integration/Commands/README.md b/tests/Integration/Commands/README.md index 6247fe412..e5e85bef2 100644 --- a/tests/Integration/Commands/README.md +++ b/tests/Integration/Commands/README.md @@ -100,8 +100,8 @@ The following local users are created and can be used by integration tests. User | Password | Permission | Description --- | --- | --- | --- -.\SqlInstall | P@ssw0rd1 | Local Windows administrator and sysadmin | Runs Setup for all instances. -.\SqlAdmin | P@ssw0rd1 | Local Windows user and sysadmin | Administrator of all SQL Server instances. +.\SqlInstall | P@ssw0rd1 | Local Windows administrator and sysadmin | Runs Setup for all the instances. +.\SqlAdmin | P@ssw0rd1 | Local Windows user and sysadmin | Administrator of all the SQL Server instances. .\svc-SqlPrimary | yig-C^Equ3 | Local Windows user. | Runs the SQL Server service. .\svc-SqlAgentPri | yig-C^Equ3 | Local Windows user. | Runs the SQL Server Agent service. .\svc-SqlSecondary | yig-C^Equ3 | Local Windows user. | Runs the SQL Server service in multi node scenarios. @@ -109,7 +109,7 @@ User | Password | Permission | Description Login | Password | Permission | Description --- | --- | --- | --- -sa | P@ssw0rd1 | sysadmin | Administrator of the Database Engine instances DSCSQLTEST. +sa | P@ssw0rd1 | sysadmin | Administrator of all the Database Engine instances. ### Image media (ISO)