From 2ea2c077f606d10a2cc4712c5582bf51f953b23e Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Wed, 12 Apr 2023 12:35:04 +0200 Subject: [PATCH] SqlServerDsc.Common: Removed the function `Invoke-Query` (#1906) - SqlServerDsc.Common - Removed the function `Invoke-Query`. It is replaced by the command `Invoke-SqlDscQuery` (issue #1902). - SqlServerDsc - New public command: - `Disconnect-SqlDscDatabaseEngine` - Disconnects from a SQL Server instance that was previously connected to using `Connect-SqlDscDatabaseEngine`. - New private command: - `ConvertTo-RedactedText` - Used to redact sensitive information from text that then can be used in console output like verbose messages. --- CHANGELOG.md | 16 + .../DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 | 6 +- .../DSC_SqlDatabaseUser.psm1 | 22 +- .../SqlServerDsc.Common.psd1 | 1 - .../SqlServerDsc.Common.psm1 | 207 +---------- .../en-US/SqlServerDsc.Common.strings.psd1 | 4 - .../sv-SE/SqlServerDsc.Common.strings.psd1 | 4 - source/Private/ConvertTo-RedactedText.ps1 | 66 ++++ source/Public/Add-SqlDscTraceFlag.ps1 | 2 +- source/Public/Disable-SqlDscAudit.ps1 | 2 +- .../Disconnect-SqlDscDatabaseEngine.ps1 | 63 ++++ source/Public/Enable-SqlDscAudit.ps1 | 2 +- source/Public/Invoke-SqlDscQuery.ps1 | 201 ++++++++-- source/Public/New-SqlDscAudit.ps1 | 2 +- source/Public/Remove-SqlDscAudit.ps1 | 2 +- source/Public/Remove-SqlDscTraceFlag.ps1 | 2 +- source/Public/Set-SqlDscAudit.ps1 | 2 +- .../Public/Set-SqlDscDatabasePermission.ps1 | 2 +- source/Public/Set-SqlDscServerPermission.ps1 | 2 +- source/Public/Set-SqlDscStartupParameter.ps1 | 2 +- source/Public/Set-SqlDscTraceFlag.ps1 | 2 +- source/en-US/SqlServerDsc.strings.psd1 | 14 + tests/Unit/DSC_SqlAGDatabase.Tests.ps1 | 236 ++++++------ tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 | 54 +-- .../Private/ConvertTo-RedactedText.Tests.ps1 | 112 ++++++ .../Disconnect-SqlDscDatabaseEngine.Tests.ps1 | 129 +++++++ .../Unit/Public/Invoke-SqlDscQuery.Tests.ps1 | 344 +++++++++++++++--- tests/Unit/SqlServerDsc.Common.Tests.ps1 | 339 +---------------- 28 files changed, 1045 insertions(+), 795 deletions(-) create mode 100644 source/Private/ConvertTo-RedactedText.ps1 create mode 100644 source/Public/Disconnect-SqlDscDatabaseEngine.ps1 create mode 100644 tests/Unit/Private/ConvertTo-RedactedText.Tests.ps1 create mode 100644 tests/Unit/Public/Disconnect-SqlDscDatabaseEngine.Tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index c72f0f634..3dd9f1982 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Remove + +- SqlServerDsc.Common + - Removed the function `Invoke-Query`. It is replaced by the command + `Invoke-SqlDscQuery` ([issue #1902](https://github.com/dsccommunity/SqlServerDsc/issues/1902)). + +### Added + +- SqlServerDsc + - New public command: + - `Disconnect-SqlDscDatabaseEngine` - Disconnects from a SQL Server instance + that was previously connected to using `Connect-SqlDscDatabaseEngine`. + - New private command: + - `ConvertTo-RedactedText` - Used to redact sensitive information from + text that then can be used in console output like verbose messages. + ## [16.2.0] - 2023-04-10 ### Added diff --git a/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 b/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 index b807121c7..e39a9d5c4 100644 --- a/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 +++ b/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 @@ -379,7 +379,7 @@ function Set-TargetResource foreach ( $databaseFileDirectory in $databaseFileDirectories ) { $fileExistsQuery = "EXEC master.dbo.xp_fileexist '$databaseFileDirectory'" - $fileExistsResult = Invoke-Query -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -Database master -Query $fileExistsQuery -WithResults + $fileExistsResult = Invoke-SqlDscQuery -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -DatabaseName 'master' -Query $fileExistsQuery -PassThru if ( $fileExistsResult.Tables.Rows.'File is a Directory' -ne 1 ) { @@ -600,8 +600,8 @@ function Set-TargetResource if ( $availabilityGroupReplica.SeedingMode -eq 'MANUAL') { # Restore the database - Invoke-Query -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -Database master -Query $restoreDatabaseQueryString -StatementTimeout $StatementTimeout - Invoke-Query -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -Database master -Query $restoreLogQueryString -StatementTimeout $StatementTimeout + Invoke-SqlDscQuery -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -DatabaseName 'master' -Query $restoreDatabaseQueryString -StatementTimeout $StatementTimeout + Invoke-SqlDscQuery -ServerName $currentAvailabilityGroupReplicaServerObject.NetName -InstanceName $currentAvailabilityGroupReplicaServerObject.ServiceName -DatabaseName 'master' -Query $restoreLogQueryString -StatementTimeout $StatementTimeout } # Add the database to the Availability Group diff --git a/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 b/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 index bfc8db6e2..f62b4097b 100644 --- a/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 @@ -219,11 +219,11 @@ function Set-TargetResource # Get-TargetResource will also help us to test if the database exist. $getTargetResourceResult = Get-TargetResource @getTargetResourceParameters - # Default parameters for the cmdlet Invoke-Query used throughout. - $invokeQueryParameters = @{ - ServerName = $ServerName - InstanceName = $InstanceName - Database = $DatabaseName + # Default parameters for the cmdlet Invoke-SqlDscQuery used throughout. + $invokeSqlDscQueryParameters = @{ + ServerName = $ServerName + InstanceName = $InstanceName + DatabaseName = $DatabaseName } $recreateDatabaseUser = $false @@ -262,7 +262,7 @@ function Set-TargetResource See remarks section in this article: https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-user-transact-sql#remarks #> - Invoke-Query @invokeQueryParameters -Query ( + Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query ( 'ALTER USER [{0}] WITH NAME = [{1}], LOGIN = [{2}];' -f $Name, $Name, $LoginName ) } @@ -338,7 +338,7 @@ function Set-TargetResource $script:localizedData.DropDatabaseUser -f $Name, $DatabaseName ) - Invoke-Query @invokeQueryParameters -Query ( + Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query ( 'DROP USER [{0}];' -f $Name ) } @@ -376,14 +376,14 @@ function Set-TargetResource # Assert that the login exist. Assert-SqlLogin @assertSqlLoginParameters - Invoke-Query @invokeQueryParameters -Query ( + Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query ( 'CREATE USER [{0}] FOR LOGIN [{1}];' -f $Name, $LoginName ) } 'NoLogin' { - Invoke-Query @invokeQueryParameters -Query ( + Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query ( 'CREATE USER [{0}] WITHOUT LOGIN;' -f $Name ) } @@ -393,7 +393,7 @@ function Set-TargetResource # Assert that the asymmetric key exist. Assert-DatabaseAsymmetricKey @PSBoundParameters - Invoke-Query @invokeQueryParameters -Query ( + Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query ( 'CREATE USER [{0}] FOR ASYMMETRIC KEY [{1}];' -f $Name, $AsymmetricKeyName ) } @@ -403,7 +403,7 @@ function Set-TargetResource # Assert that the certificate exist. Assert-DatabaseCertificate @PSBoundParameters - Invoke-Query @invokeQueryParameters -Query ( + Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query ( 'CREATE USER [{0}] FOR CERTIFICATE [{1}];' -f $Name, $CertificateName ) } diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 index 6f6847ab0..e73d52d1b 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 @@ -34,7 +34,6 @@ 'Get-SqlInstanceMajorVersion' 'Restart-SqlService' 'Restart-ReportingServicesService' - 'Invoke-Query' 'Update-AvailabilityGroupReplica' 'Test-LoginEffectivePermissions' 'Test-AvailabilityReplicaSeedingModeAutomatic' diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index bd26e087a..43d7631c7 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -1266,197 +1266,6 @@ function Restart-ReportingServicesService } } - -<# - .SYNOPSIS - Executes a query on the specified database. - - .PARAMETER ServerName - The hostname of the server that hosts the SQL instance. - - .PARAMETER InstanceName - The name of the SQL instance that hosts the database. - - .PARAMETER Database - Specify the name of the database to execute the query on. - - .PARAMETER Query - The query string to execute. - - .PARAMETER DatabaseCredential - PSCredential object with the credentials to use to impersonate a user - when connecting. If this is not provided then the current user will be - used to connect to the SQL Server Database Engine instance. - - .PARAMETER LoginType - Specifies which type of logon credential should be used. The valid types are - Integrated, WindowsUser, and SqlLogin. If WindowsUser or SqlLogin are specified - then the SetupCredential needs to be specified as well. - - .PARAMETER SqlServerObject - You can pass in an object type of 'Microsoft.SqlServer.Management.Smo.Server'. - This can also be passed in through the pipeline. See examples. - - .PARAMETER WithResults - Specifies if the query should return results. - - .PARAMETER StatementTimeout - Set the query StatementTimeout in seconds. Default 600 seconds (10mins). - - .PARAMETER RedactText - One or more strings to redact from the query when verbose messages are - written to the console. Strings here will be escaped so they will not - be interpreted as regular expressions (RegEx). - - .EXAMPLE - Invoke-Query -ServerName Server1 -InstanceName MSSQLSERVER -Database master ` - -Query 'SELECT name FROM sys.databases' -WithResults - - .EXAMPLE - Invoke-Query -ServerName Server1 -InstanceName MSSQLSERVER -Database master ` - -Query 'RESTORE DATABASE [NorthWinds] WITH RECOVERY' - - .EXAMPLE - Connect-SQL @sqlConnectionParameters | Invoke-Query -Database master ` - -Query 'SELECT name FROM sys.databases' -WithResults - - .EXAMPLE - Invoke-Query -ServerName Server1 -InstanceName MSSQLSERVER -Database MyDatabase ` - -Query "select * from MyTable where password = 'PlaceholderPa\ssw0rd1' and password = 'placeholder secret passphrase'" ` - -WithResults -RedactText @('PlaceholderPa\sSw0rd1','Placeholder Secret PassPhrase') -Verbose - - .NOTES - The script analyzer rule UseSyntacticallyCorrectExamples will fail in VS Code - on this function unless the SMO types are loaded (either the real ones or the - stubs). When the rule is run in the pipeline the test will load the SMO stub - classes for proper testing of the rule. -#> -function Invoke-Query -{ - [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when the output 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(DefaultParameterSetName = 'SqlServer')] - param - ( - [Parameter(ParameterSetName = 'SqlServer')] - [ValidateNotNullOrEmpty()] - [System.String] - $ServerName = (Get-ComputerName), - - [Parameter(ParameterSetName = 'SqlServer')] - [System.String] - $InstanceName = 'MSSQLSERVER', - - [Parameter(Mandatory = $true)] - [System.String] - $Database, - - [Parameter(Mandatory = $true)] - [System.String] - $Query, - - [Parameter()] - [Alias('SetupCredential')] - [System.Management.Automation.PSCredential] - $DatabaseCredential, - - [Parameter()] - [ValidateSet('Integrated', 'WindowsUser', 'SqlLogin')] - [System.String] - $LoginType = 'Integrated', - - [Parameter(ValueFromPipeline, ParameterSetName = 'SqlObject', Mandatory = $true)] - [ValidateNotNull()] - [Microsoft.SqlServer.Management.Smo.Server] - $SqlServerObject, - - [Parameter()] - [Switch] - $WithResults, - - [Parameter()] - [ValidateNotNull()] - [System.Int32] - $StatementTimeout = 600, - - [Parameter()] - [System.String[]] - $RedactText - ) - - if ($PSCmdlet.ParameterSetName -eq 'SqlObject') - { - $serverObject = $SqlServerObject - } - elseif ($PSCmdlet.ParameterSetName -eq 'SqlServer') - { - $connectSQLParameters = @{ - ServerName = $ServerName - InstanceName = $InstanceName - StatementTimeout = $StatementTimeout - } - - if ($LoginType -ne 'Integrated') - { - $connectSQLParameters['LoginType'] = $LoginType - } - - if ($PSBoundParameters.ContainsKey('DatabaseCredential')) - { - $connectSQLParameters.SetupCredential = $DatabaseCredential - } - - $serverObject = Connect-SQL @connectSQLParameters -ErrorAction 'Stop' - } - - $redactedQuery = $Query - - foreach ($redactString in $RedactText) - { - <# - Escaping the string to handle strings which could look like - regular expressions, like passwords. - #> - $escapedRedactedString = [System.Text.RegularExpressions.Regex]::Escape($redactString) - - $redactedQuery = $redactedQuery -ireplace $escapedRedactedString, '*******' - } - - if ($WithResults) - { - try - { - Write-Verbose -Message ( - $script:localizedData.ExecuteQueryWithResults -f $redactedQuery - ) -Verbose - - $result = $serverObject.Databases[$Database].ExecuteWithResults($Query) - } - catch - { - $errorMessage = $script:localizedData.ExecuteQueryWithResultsFailed -f $Database - New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ - } - } - else - { - try - { - Write-Verbose -Message ( - $script:localizedData.ExecuteNonQuery -f $redactedQuery - ) -Verbose - - $serverObject.Databases[$Database].ExecuteNonQuery($Query) - } - catch - { - $errorMessage = $script:localizedData.ExecuteNonQueryFailed -f $Database - New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ - } - } - - return $result -} - <# .SYNOPSIS Executes the alter method on an Availability Group Replica object. @@ -1564,11 +1373,11 @@ function Test-LoginEffectivePermissions # Assume the permissions are not present $permissionsPresent = $false - $invokeQueryParameters = @{ + $invokeSqlDscQueryParameters = @{ ServerName = $ServerName InstanceName = $InstanceName - Database = 'master' - WithResults = $true + DatabaseName = 'master' + PassThru = $true } if ( [System.String]::IsNullOrEmpty($SecurableName) ) @@ -1592,7 +1401,7 @@ function Test-LoginEffectivePermissions Write-Verbose -Message ($script:localizedData.GetEffectivePermissionForLogin -f $LoginName, $InstanceName) -Verbose - $loginEffectivePermissionsResult = Invoke-Query @invokeQueryParameters -Query $queryToGetEffectivePermissionsForLogin + $loginEffectivePermissionsResult = Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query $queryToGetEffectivePermissionsForLogin $loginEffectivePermissions = $loginEffectivePermissionsResult.Tables.Rows.permission_name if ( $null -ne $loginEffectivePermissions ) @@ -1658,11 +1467,11 @@ function Test-AvailabilityReplicaSeedingModeAutomatic # Only check the seeding mode if this is SQL 2016 or newer if ( $serverObject.Version -ge 13 ) { - $invokeQueryParams = @{ + $invokeSqlDscQueryParameters = @{ ServerName = $ServerName InstanceName = $InstanceName - Database = 'master' - WithResults = $true + DatabaseName = 'master' + PassThru = $true } $queryToGetSeedingMode = " @@ -1672,7 +1481,7 @@ function Test-AvailabilityReplicaSeedingModeAutomatic WHERE ag.name = '$AvailabilityGroupName' AND ar.replica_server_name = '$AvailabilityReplicaName' " - $seedingModeResults = Invoke-Query @invokeQueryParams -Query $queryToGetSeedingMode + $seedingModeResults = Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query $queryToGetSeedingMode $seedingMode = $seedingModeResults.Tables.Rows.seeding_mode_desc if ( $seedingMode -eq 'Automatic' ) diff --git a/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 b/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 index 7a6be1fc4..fad3d2d82 100644 --- a/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 +++ b/source/Modules/SqlServerDsc.Common/en-US/SqlServerDsc.Common.strings.psd1 @@ -29,8 +29,6 @@ ConvertFrom-StringData @' StartingDependentService = Starting service '{0}'. (SQLCOMMON0042) WaitingInstanceTimeout = Waiting for instance {0}\\{1} to report status online, with a timeout value of {2} seconds. (SQLCOMMON0043) FailedToConnectToInstanceTimeout = Failed to connect to the instance {0}\\{1} within the timeout period of {2} seconds. (SQLCOMMON0044) - ExecuteQueryWithResultsFailed = Executing query with results failed on database '{0}'. (SQLCOMMON0045) - ExecuteNonQueryFailed = Executing non-query failed on database '{0}'. (SQLCOMMON0046) AlterAvailabilityGroupReplicaFailed = Failed to alter the availability group replica '{0}'. (SQLCOMMON0047) GetEffectivePermissionForLogin = Getting the effective permissions for the login '{0}' on '{1}'. (SQLCOMMON0048) ClusterPermissionsMissing = The cluster does not have permissions to manage the Availability Group on '{0}\\{1}'. Grant 'Connect SQL', 'Alter Any Availability Group', and 'View Server State' to either 'NT SERVICE\\ClusSvc' or 'NT AUTHORITY\\SYSTEM'. (SQLCOMMON0049) @@ -40,8 +38,6 @@ ConvertFrom-StringData @' ClusterLoginPermissionsPresent = The cluster login '{0}' has the required permissions. (SQLCOMMON0053) ConnectingUsingIntegrated = Connecting as current user '{0}' using integrated security. (SQLCOMMON0054) ConnectingUsingImpersonation = Impersonate credential '{0}' with login type '{1}'. (SQLCOMMON0056) - ExecuteQueryWithResults = Returning the results of the query `{0}`. (SQLCOMMON0057) - ExecuteNonQuery = Executing the query `{0}`. (SQLCOMMON0058) ClusterResourceNotFoundOrOffline = The SQL Server cluster resource '{0}' was not found or the resource has been taken offline. (SQLCOMMON0066) NotOwnerOfClusterResource = The node '{0}' is not the owner of the cluster resource '{1}'. The owner is '{2}' so no restart is needed. (SQLCOMMON0067) LoadedAssembly = Loaded the assembly '{0}'. (SQLCOMMON0068) diff --git a/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 b/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 index 305aeae20..269fd2ee4 100644 --- a/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 +++ b/source/Modules/SqlServerDsc.Common/sv-SE/SqlServerDsc.Common.strings.psd1 @@ -35,8 +35,6 @@ ConvertFrom-StringData @' StartingDependentService = Startar tjänst {0} (SQLCOMMON0042) WaitingInstanceTimeout = Waiting for instance {0}\\{1} to report status online, with a timeout value of {2} seconds. (SQLCOMMON0043) FailedToConnectToInstanceTimeout = Failed to connect to the instance {0}\\{1} within the timeout period of {2} seconds. (SQLCOMMON0044) - ExecuteQueryWithResultsFailed = Exekvering av fråga med resultat misslyckades mot databas '{0}'. (SQLCOMMON0045) - ExecuteNonQueryFailed = Exekvering av icke-fråga misslyckades på databas '{0}'. (SQLCOMMON0046) AlterAvailabilityGroupReplicaFailed = Misslyckades att ändra Availability Group kopia '{0}'. (SQLCOMMON0047) GetEffectivePermissionForLogin = Hämtar effektiva behörigheter för inloggningen '{0}' på '{1}'. (SQLCOMMON0048) ClusterPermissionsMissing = The cluster does not have permissions to manage the Availability Group on '{0}\\{1}'. Grant 'Connect SQL', 'Alter Any Availability Group', and 'View Server State' to either 'NT SERVICE\\ClusSvc' or 'NT AUTHORITY\\SYSTEM'. (SQLCOMMON0049) @@ -46,8 +44,6 @@ ConvertFrom-StringData @' ClusterLoginPermissionsPresent = The cluster login '{0}' has the required permissions. (SQLCOMMON0053) ConnectingUsingIntegrated = Anslutning som nuvarande användare '{0}' med integrerad säkerhet. (SQLCOMMON0054) ConnectingUsingImpersonation = Uppträder som behörigheten '{0}' med inloggningstyp '{1}'. (SQLCOMMON0056) - ExecuteQueryWithResults = Returnerar resultatet av frågan `{0}`. (SQLCOMMON0057) - ExecuteNonQuery = Exekverar frågan `{0}`. (SQLCOMMON0058) ClusterResourceNotFoundOrOffline = The SQL Server cluster resource '{0}' was not found or the resource has been taken offline. (SQLCOMMON0066) NotOwnerOfClusterResource = The node '{0}' is not the owner of the cluster resource '{1}'. The owner is '{2}' so no restart is needed. (SQLCOMMON0067) LoadedAssembly = Loaded the assembly '{0}'. (SQLCOMMON0068) diff --git a/source/Private/ConvertTo-RedactedText.ps1 b/source/Private/ConvertTo-RedactedText.ps1 new file mode 100644 index 000000000..71d9701a0 --- /dev/null +++ b/source/Private/ConvertTo-RedactedText.ps1 @@ -0,0 +1,66 @@ +<# + .SYNOPSIS + Redacts a text from one or more specified phrases. + + .DESCRIPTION + Redacts a text using best effort from one or more specified phrases. For + it to work the sensitiv phrases must be known and passed into the parameter + RedactText. If any single character in a phrase is wrong the sensitiv + information will not be redacted. The redaction is case-insensitive. + + .PARAMETER Text + Specifies the text that will be redacted. + + .PARAMETER RedactPhrase + Specifies one or more phrases to redact from the text. Text strings will + be escaped so they will not be interpreted as regular expressions (RegEx). + + .PARAMETER RedactWith + Specifies a phrase that will be used as redaction. + + .EXAMPLE + ConvertTo-RedactedText -Text 'My secret phrase: secret123' -RedactPhrase 'secret123' + + Returns the text with the phrases redacted with the default redaction phrase. + + .EXAMPLE + ConvertTo-RedactedText -Text 'My secret phrase: secret123' -RedactPhrase 'secret123' -RedactWith '----' + + Returns the text with the phrases redacted to '----'. +#> +function ConvertTo-RedactedText +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [System.String] + $Text, + + [Parameter(Mandatory = $true)] + [System.String[]] + $RedactPhrase, + + [Parameter()] + [System.String] + $RedactWith = '*******' + ) + + process + { + $redactedText = $Text + + foreach ($redactString in $RedactPhrase) + { + <# + Escaping the string to handle strings which could look like + regular expressions, like passwords. + #> + $escapedRedactedString = [System.Text.RegularExpressions.Regex]::Escape($redactString) + + $redactedText = $redactedText -ireplace $escapedRedactedString, $RedactWith # cSpell: ignore ireplace + } + + return $redactedText + } +} diff --git a/source/Public/Add-SqlDscTraceFlag.ps1 b/source/Public/Add-SqlDscTraceFlag.ps1 index ab6e096da..cd77b86cf 100644 --- a/source/Public/Add-SqlDscTraceFlag.ps1 +++ b/source/Public/Add-SqlDscTraceFlag.ps1 @@ -19,7 +19,7 @@ Specifies the trace flags to add. .PARAMETER Force - Specifies that the trace flag should be added with out any confirmation. + Specifies that the trace flag should be added without any confirmation. .EXAMPLE Add-SqlDscTraceFlag -TraceFlag 4199 diff --git a/source/Public/Disable-SqlDscAudit.ps1 b/source/Public/Disable-SqlDscAudit.ps1 index c6ff1423f..fd9856891 100644 --- a/source/Public/Disable-SqlDscAudit.ps1 +++ b/source/Public/Disable-SqlDscAudit.ps1 @@ -15,7 +15,7 @@ Specifies the name of the server audit to be disabled. .PARAMETER Force - Specifies that the audit should be disabled with out any confirmation. + Specifies that the audit should be disabled without any confirmation. .PARAMETER Refresh Specifies that the **ServerObject**'s audits should be refreshed before diff --git a/source/Public/Disconnect-SqlDscDatabaseEngine.ps1 b/source/Public/Disconnect-SqlDscDatabaseEngine.ps1 new file mode 100644 index 000000000..ea2a2a4db --- /dev/null +++ b/source/Public/Disconnect-SqlDscDatabaseEngine.ps1 @@ -0,0 +1,63 @@ +<# + .SYNOPSIS + Disconnect from a SQL Server Database Engine instance. + + .DESCRIPTION + Disconnect from a SQL Server Database Engine instance. + + .PARAMETER ServerObject + Specifies a current server connection object. + + .PARAMETER Force + Specifies that there is no confirmation before disconnect. + + .EXAMPLE + $serverObject = Connect-SqlDscDatabaseEngine + Disconnect-SqlDscDatabaseEngine -ServerObject $serverObject + + Connects and then disconnects from the default instance on the local server. + + .EXAMPLE + Connect-SqlDscDatabaseEngine | Disconnect-SqlDscDatabaseEngine + + Connects and then disconnects from the default instance on the local server. + + .OUTPUTS + None. +#> +function Disconnect-SqlDscDatabaseEngine +{ + [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([System.Data.DataSet])] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] + param + ( + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [Microsoft.SqlServer.Management.Smo.Server] + $ServerObject, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $Force + ) + + begin + { + if ($Force.IsPresent) + { + $ConfirmPreference = 'None' + } + } + + process + { + $verboseDescriptionMessage = $script:localizedData.DatabaseEngine_Disconnect_ShouldProcessVerboseDescription -f $ServerObject.InstanceName + $verboseWarningMessage = $script:localizedData.DatabaseEngine_Disconnect_ShouldProcessVerboseWarning -f $ServerObject.InstanceName + $captionMessage = $script:localizedData.DatabaseEngine_Disconnect_ShouldProcessCaption + + if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage)) + { + $ServerObject.ConnectionContext.Disconnect() + } + } +} diff --git a/source/Public/Enable-SqlDscAudit.ps1 b/source/Public/Enable-SqlDscAudit.ps1 index e2682faf2..4e234a9c3 100644 --- a/source/Public/Enable-SqlDscAudit.ps1 +++ b/source/Public/Enable-SqlDscAudit.ps1 @@ -15,7 +15,7 @@ Specifies the name of the server audit to be enabled. .PARAMETER Force - Specifies that the audit should be enabled with out any confirmation. + Specifies that the audit should be enabled without any confirmation. .PARAMETER Refresh Specifies that the **ServerObject**'s audits should be refreshed before diff --git a/source/Public/Invoke-SqlDscQuery.ps1 b/source/Public/Invoke-SqlDscQuery.ps1 index ae44cc7e5..929af7c95 100644 --- a/source/Public/Invoke-SqlDscQuery.ps1 +++ b/source/Public/Invoke-SqlDscQuery.ps1 @@ -8,8 +8,25 @@ .PARAMETER ServerObject Specifies current server connection object. + .PARAMETER ServerName + Specifies the server name where the instance exist. + + .PARAMETER InstanceName + Specifies the instance name on which to execute the T-SQL query. + + .PARAMETER Credential + Specifies the credentials to use to impersonate a user when connecting. + If this is not provided then the current user will be used to connect + to the SQL Server Database Engine instance. + + .PARAMETER LoginType + Specifies which type of credentials are specified. The valid types are + Integrated, WindowsUser, and SqlLogin. If WindowsUser or SqlLogin are + specified then the Credential needs to be specified as well. Defaults + to `Integrated`. + .PARAMETER DatabaseName - Specify the name of the database to execute the query on. + Specifies the name of the database to execute the T-SQL query in. .PARAMETER Query The query string to execute. @@ -21,53 +38,84 @@ Set the query StatementTimeout in seconds. Default 600 seconds (10 minutes). .PARAMETER RedactText - One or more strings to redact from the query when verbose messages are - written to the console. Strings here will be escaped so they will not + One or more text strings to redact from the query when verbose messages + are written to the console. Strings will be escaped so they will not be interpreted as regular expressions (RegEx). + .PARAMETER Force + Specifies that the query should be executed without any confirmation. + .OUTPUTS `[System.Data.DataSet]` when passing parameter **PassThru**, otherwise outputs none. .EXAMPLE - $serverInstance = Connect-SqlDscDatabaseEngine - Invoke-SqlDscQuery -ServerObject $serverInstance -Database master ` + $serverObject = Connect-SqlDscDatabaseEngine + Invoke-SqlDscQuery -ServerObject $serverObject -DatabaseName 'master' ` -Query 'SELECT name FROM sys.databases' -PassThru - Runs the query and returns all the database names in the instance. + Connects to the default instance and then runs a query to return all the + database names in the instance. .EXAMPLE - $serverInstance = Connect-SqlDscDatabaseEngine - Invoke-SqlDscQuery -ServerObject $serverInstance -Database master ` + $serverObject = Connect-SqlDscDatabaseEngine + $serverObject | Invoke-SqlDscQuery -DatabaseName 'master' ` -Query 'RESTORE DATABASE [NorthWinds] WITH RECOVERY' - Runs the query to restores the database NorthWinds. + Connects to the default instance and then runs the query to restore the + database NorthWinds. .EXAMPLE - $serverInstance = Connect-SqlDscDatabaseEngine - Invoke-SqlDscQuery -ServerObject $serverInstance -Database master ` + $serverObject = Connect-SqlDscDatabaseEngine + Invoke-SqlDscQuery -ServerObject $serverObject -DatabaseName 'master' ` -Query "select * from MyTable where password = 'PlaceholderPa\ssw0rd1' and password = 'placeholder secret passphrase'" ` - -PassThru -RedactText @('PlaceholderPa\sSw0rd1','Placeholder Secret PassPhrase') ` - -Verbose + -RedactText @('PlaceholderPa\sSw0rd1','Placeholder Secret PassPhrase') ` + -PassThru -Verbose Shows how to redact sensitive information in the query when the query string - is output as verbose information when the parameter Verbose is used. + is output as verbose information when the parameter Verbose is used. For it + to work the sensitiv information must be known and passed into the parameter + RedactText. If any single character is wrong the sensitiv information will + not be redacted. The redaction is case-insensitive. + + .EXAMPLE + Invoke-SqlDscQuery -ServerName Server1 -InstanceName MSSQLSERVER -DatabaseName 'master' ` + -Query 'SELECT name FROM sys.databases' -PassThru - .NOTES - This is a wrapper for private function Invoke-Query, until it move into - this public function. + Connects to the default instance and then runs a query to return all the + database names in the instance. #> function Invoke-SqlDscQuery { [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([System.Data.DataSet])] - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = 'ByServerName', SupportsShouldProcess = $true, ConfirmImpact = 'High')] param ( - [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [Parameter(ParameterSetName = 'ByServerObject', Mandatory = $true, ValueFromPipeline = $true)] [Microsoft.SqlServer.Management.Smo.Server] $ServerObject, + [Parameter(ParameterSetName = 'ByServerName')] + [ValidateNotNullOrEmpty()] + [System.String] + $ServerName = (Get-ComputerName), + + [Parameter(ParameterSetName = 'ByServerName')] + [System.String] + $InstanceName = 'MSSQLSERVER', + + [Parameter(ParameterSetName = 'ByServerName')] + [Alias('SetupCredential')] + [Alias('DatabaseCredential')] + [System.Management.Automation.PSCredential] + $Credential, + + [Parameter(ParameterSetName = 'ByServerName')] + [ValidateSet('Integrated', 'WindowsUser', 'SqlLogin')] + [System.String] + $LoginType = 'Integrated', + [Parameter(Mandatory = $true)] [System.String] $DatabaseName, @@ -77,6 +125,7 @@ function Invoke-SqlDscQuery $Query, [Parameter()] + [Alias('WithResults')] [Switch] $PassThru, @@ -86,33 +135,121 @@ function Invoke-SqlDscQuery $StatementTimeout = 600, [Parameter()] + [ValidateNotNullOrEmpty()] [System.String[]] - $RedactText + $RedactText, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $Force ) - process + begin { - $invokeQueryParameters = @{ - SqlServerObject = $ServerObject - Database = $DatabaseName - Query = $Query + if ($Force.IsPresent) + { + $ConfirmPreference = 'None' } - if ($PSBoundParameters.ContainsKey('PassThru')) + if ($PSCmdlet.ParameterSetName -eq 'ByServerName') { - $invokeQueryParameters.WithResults = $PassThru + $connectSqlDscDatabaseEngineParameters = @{ + ServerName = $ServerName + InstanceName = $InstanceName + StatementTimeout = $StatementTimeout + ErrorAction = 'Stop' + Verbose = $VerbosePreference + } + + if ($LoginType -ne 'Integrated') + { + $connectSqlDscDatabaseEngineParameters['LoginType'] = $LoginType + } + + if ($PSBoundParameters.ContainsKey('Credential')) + { + $connectSqlDscDatabaseEngineParameters.Credential = $Credential + } + + $ServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters } - if ($PSBoundParameters.ContainsKey('StatementTimeout')) + if ($PSBoundParameters.ContainsKey('RedactText')) { - $invokeQueryParameters.StatementTimeout = $StatementTimeout + $redactedQuery = ConvertTo-RedactedText -Text $Query -RedactPhrase $RedactText } + } - if ($PSBoundParameters.ContainsKey('RedactText')) + process + { + $result = $null + + $verboseDescriptionMessage = $script:localizedData.Query_Invoke_ShouldProcessVerboseDescription -f $InstanceName + $verboseWarningMessage = $script:localizedData.Query_Invoke_ShouldProcessVerboseWarning -f $InstanceName + $captionMessage = $script:localizedData.Query_Invoke_ShouldProcessCaption + + if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage)) { - $invokeQueryParameters.RedactText = $RedactText + $previousStatementTimeout = $null + + if ($PSCmdlet.ParameterSetName -eq 'ByServerObject') + { + if ($PSBoundParameters.ContainsKey('StatementTimeout')) + { + # Make sure we can return the StatementTimeout before exiting. + $previousStatementTimeout = $ServerObject.ConnectionContext.StatementTimeout + + $ServerObject.ConnectionContext.StatementTimeout = $StatementTimeout + } + } + + try + { + if ($PassThru) + { + Write-Verbose -Message ( + $script:localizedData.Query_Invoke_ExecuteQueryWithResults -f $redactedQuery + ) + + $result = $ServerObject.Databases[$DatabaseName].ExecuteWithResults($Query) + + return $result + } + else + { + Write-Verbose -Message ( + $script:localizedData.Query_Invoke_ExecuteNonQuery -f $redactedQuery + ) + + $null = $ServerObject.Databases[$DatabaseName].ExecuteNonQuery($Query) + } + } + catch + { + $writeErrorParameters = @{ + Message = $_.Exception.ToString() + Category = 'InvalidOperation' + ErrorId = 'ISDQ0001' # cSpell: disable-line + TargetObject = $DatabaseName + } + + Write-Error @writeErrorParameters + } + finally + { + if ($previousStatementTimeout) + { + $ServerObject.ConnectionContext.StatementTimeout = $previousStatementTimeout + } + } } + } - return (Invoke-Query @invokeQueryParameters) + end + { + if ($PSCmdlet.ParameterSetName -eq 'ByServerName') + { + $ServerObject | Disconnect-SqlDscDatabaseEngine -Force -Verbose:$VerbosePreference + } } } diff --git a/source/Public/New-SqlDscAudit.ps1 b/source/Public/New-SqlDscAudit.ps1 index 5706491ab..4bea28726 100644 --- a/source/Public/New-SqlDscAudit.ps1 +++ b/source/Public/New-SqlDscAudit.ps1 @@ -29,7 +29,7 @@ as database mirroring an audit needs a specific GUID. .PARAMETER Force - Specifies that the audit should be created with out any confirmation. + Specifies that the audit should be created without any confirmation. .PARAMETER Refresh Specifies that the **ServerObject**'s audits should be refreshed before diff --git a/source/Public/Remove-SqlDscAudit.ps1 b/source/Public/Remove-SqlDscAudit.ps1 index e872ae439..c3681b679 100644 --- a/source/Public/Remove-SqlDscAudit.ps1 +++ b/source/Public/Remove-SqlDscAudit.ps1 @@ -15,7 +15,7 @@ Specifies the name of the server audit to be removed. .PARAMETER Force - Specifies that the audit should be removed with out any confirmation. + Specifies that the audit should be removed without any confirmation. .PARAMETER Refresh Specifies that the **ServerObject**'s audits should be refreshed before diff --git a/source/Public/Remove-SqlDscTraceFlag.ps1 b/source/Public/Remove-SqlDscTraceFlag.ps1 index 7e2f24309..911b6b08e 100644 --- a/source/Public/Remove-SqlDscTraceFlag.ps1 +++ b/source/Public/Remove-SqlDscTraceFlag.ps1 @@ -19,7 +19,7 @@ Specifies the trace flags to remove. .PARAMETER Force - Specifies that the trace flag should be removed with out any confirmation. + Specifies that the trace flag should be removed without any confirmation. .EXAMPLE Remove-SqlDscTraceFlag -TraceFlag 4199 diff --git a/source/Public/Set-SqlDscAudit.ps1 b/source/Public/Set-SqlDscAudit.ps1 index 7083d7afd..5c16b0279 100644 --- a/source/Public/Set-SqlDscAudit.ps1 +++ b/source/Public/Set-SqlDscAudit.ps1 @@ -33,7 +33,7 @@ as database mirroring an audit needs a specific GUID. .PARAMETER Force - Specifies that the audit should be updated with out any confirmation. + Specifies that the audit should be updated without any confirmation. .PARAMETER Refresh Specifies that the audit object should be refreshed before updating. This diff --git a/source/Public/Set-SqlDscDatabasePermission.ps1 b/source/Public/Set-SqlDscDatabasePermission.ps1 index 2bd25c7fe..0ba7665d3 100644 --- a/source/Public/Set-SqlDscDatabasePermission.ps1 +++ b/source/Public/Set-SqlDscDatabasePermission.ps1 @@ -29,7 +29,7 @@ and the revocation will cascade. .PARAMETER Force - Specifies that the permissions should be set with out any confirmation. + Specifies that the permissions should be set without any confirmation. .OUTPUTS None. diff --git a/source/Public/Set-SqlDscServerPermission.ps1 b/source/Public/Set-SqlDscServerPermission.ps1 index 229e4ddf2..69b086f0b 100644 --- a/source/Public/Set-SqlDscServerPermission.ps1 +++ b/source/Public/Set-SqlDscServerPermission.ps1 @@ -26,7 +26,7 @@ and the revocation will cascade. .PARAMETER Force - Specifies that the permissions should be set with out any confirmation. + Specifies that the permissions should be set without any confirmation. .OUTPUTS None. diff --git a/source/Public/Set-SqlDscStartupParameter.ps1 b/source/Public/Set-SqlDscStartupParameter.ps1 index 38e2f94f4..7f83d3c4a 100644 --- a/source/Public/Set-SqlDscStartupParameter.ps1 +++ b/source/Public/Set-SqlDscStartupParameter.ps1 @@ -25,7 +25,7 @@ only by SQL Server support engineers." .PARAMETER Force - Specifies that the startup parameters should be set with out any confirmation. + Specifies that the startup parameters should be set without any confirmation. .EXAMPLE Set-SqlDscStartupParameters -TraceFlag 4199 diff --git a/source/Public/Set-SqlDscTraceFlag.ps1 b/source/Public/Set-SqlDscTraceFlag.ps1 index 1786fe1a3..6d28d0f8c 100644 --- a/source/Public/Set-SqlDscTraceFlag.ps1 +++ b/source/Public/Set-SqlDscTraceFlag.ps1 @@ -19,7 +19,7 @@ Specifies the trace flags to set. .PARAMETER Force - Specifies that the trace flag should be set with out any confirmation. + Specifies that the trace flag should be set without any confirmation. .EXAMPLE Set-SqlDscTraceFlag -TraceFlag 4199 diff --git a/source/en-US/SqlServerDsc.strings.psd1 b/source/en-US/SqlServerDsc.strings.psd1 index 08c714f31..8427318d2 100644 --- a/source/en-US/SqlServerDsc.strings.psd1 +++ b/source/en-US/SqlServerDsc.strings.psd1 @@ -158,4 +158,18 @@ ConvertFrom-StringData @' PreferredModule_PushingLocation = SQLPS module changes CWD to SQLServer:\ when loading, pushing location to pop it when module is loaded. PreferredModule_PoppingLocation = Popping location back to what it was before importing SQLPS module. PreferredModule_FailedFinding = Failed to find a dependent module. Unable to run SQL Server commands or use SQL Server types. Please install the {0} or SQLPS then try to import SqlServerDsc again. + + ## Invoke-SqlDscQuery + Query_Invoke_ShouldProcessVerboseDescription = Executing a Transact-SQL query on the instance '{0}'. + Query_Invoke_ShouldProcessVerboseWarning = Are you sure you want to execute the Transact-SQL script on the instance '{0}'? + # This string shall not end with full stop (.) since it is used as a title of ShouldProcess messages. + Query_Invoke_ShouldProcessCaption = Execute Transact-SQL script on instance + Query_Invoke_ExecuteQueryWithResults = Returning the results of the query `{0}`. + Query_Invoke_ExecuteNonQuery = Executing the query `{0}`. + + ## Disconnect-SqlDscDatabaseEngine + DatabaseEngine_Disconnect_ShouldProcessVerboseDescription = Disconnecting from the instance '{0}'. + DatabaseEngine_Disconnect_ShouldProcessVerboseWarning = Are you sure you want to disconnect from the instance '{0}'? + # This string shall not end with full stop (.) since it is used as a title of ShouldProcess messages. + DatabaseEngine_Disconnect_ShouldProcessCaption = Disconnect from instance '@ diff --git a/tests/Unit/DSC_SqlAGDatabase.Tests.ps1 b/tests/Unit/DSC_SqlAGDatabase.Tests.ps1 index 7f06a4f87..a3b6f8d73 100644 --- a/tests/Unit/DSC_SqlAGDatabase.Tests.ps1 +++ b/tests/Unit/DSC_SqlAGDatabase.Tests.ps1 @@ -773,8 +773,8 @@ REVERT' Mock -CommandName Get-PrimaryReplicaServerObject -MockWith { return $mockServerObject } -Verifiable -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Mock -CommandName Get-PrimaryReplicaServerObject -MockWith { return $mockServer2Object } -Verifiable -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Mock -CommandName Import-SqlDscPreferredModule -Verifiable - Mock -CommandName Invoke-Query -Verifiable -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Mock -CommandName Invoke-Query -Verifiable -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Mock -CommandName Invoke-SqlDscQuery -Verifiable -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Mock -CommandName invokeSqlDscQueryParameters -Verifiable -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Mock -CommandName Join-Path -MockWith { [IO.Path]::Combine($databaseMembershipClass.BackupPath,"$($database.Name)_Full_$(Get-Date -Format 'yyyyMMddhhmmss').bak") } -Verifiable -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Mock -CommandName Join-Path -MockWith { [IO.Path]::Combine($databaseMembershipClass.BackupPath,"$($database.Name)_Log_$(Get-Date -Format 'yyyyMMddhhmmss').trn") } -Verifiable -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Mock -CommandName Remove-Item -Verifiable @@ -802,7 +802,7 @@ REVERT' Mock -CommandName Connect-SQL -MockWith { return $mockServerObject } -Verifiable -ParameterFilter { $ServerName -eq 'Server1' -and $InstanceName -eq 'MSSQLSERVER' } Mock -CommandName Connect-SQL -MockWith { return $mockServerObject } -Verifiable -ParameterFilter { $ServerName -eq 'Server1' } Mock -CommandName Connect-SQL -MockWith { return $mockServer2Object } -Verifiable -ParameterFilter { $ServerName -eq 'Server2' } - Mock -CommandName Invoke-Query -MockWith $mockResultInvokeQueryFileExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockResultInvokeQueryFileExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } Mock -CommandName Remove-SqlAvailabilityDatabase -Verifiable Mock -CommandName Test-ImpersonatePermissions -MockWith { $true } -Verifiable } @@ -823,9 +823,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly @@ -850,9 +850,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly @@ -877,9 +877,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -904,9 +904,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly @@ -932,9 +932,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly @@ -959,9 +959,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1010,9 +1010,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1040,9 +1040,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1076,9 +1076,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1107,9 +1107,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1120,7 +1120,7 @@ REVERT' } It 'Should throw the correct error when the database file path does not exist on the secondary replica' { - Mock -CommandName Invoke-Query -MockWith $mockResultInvokeQueryFileNotExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockResultInvokeQueryFileNotExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } $originalValue = $mockServer2Object.Databases['DB1'].FileGroups.Files.FileName $mockServer2Object.Databases['DB1'].FileGroups.Files.FileName = ( [IO.Path]::Combine( 'X:\', "DB1.mdf" ) ) @@ -1138,9 +1138,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1151,7 +1151,7 @@ REVERT' } It 'Should throw the correct error when the log file path does not exist on the secondary replica' { - Mock -CommandName Invoke-Query -MockWith $mockResultInvokeQueryFileNotExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockResultInvokeQueryFileNotExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } $originalValue = $mockServer2Object.Databases['DB1'].LogFiles.FileName $mockServer2Object.Databases['DB1'].LogFiles.FileName = ( [IO.Path]::Combine( 'Y:\', "DB1.ldf" ) ) @@ -1169,9 +1169,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1200,9 +1200,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1231,9 +1231,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly @@ -1258,9 +1258,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1285,9 +1285,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1312,9 +1312,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1339,9 +1339,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly @@ -1371,9 +1371,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly @@ -1833,8 +1833,8 @@ REVERT' Mock -CommandName Get-PrimaryReplicaServerObject -MockWith { return $mockServerObject } -Verifiable -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Mock -CommandName Get-PrimaryReplicaServerObject -MockWith { return $mockServer2Object } -Verifiable -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Mock -CommandName Import-SqlDscPreferredModule -Verifiable - Mock -CommandName Invoke-Query -Verifiable -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Mock -CommandName Invoke-Query -Verifiable -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Mock -CommandName Invoke-SqlDscQuery -Verifiable -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Mock -CommandName Invoke-SqlDscQuery -Verifiable -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Mock -CommandName Join-Path -MockWith { [IO.Path]::Combine($databaseMembershipClass.BackupPath,"$($database.Name)_Full_$(Get-Date -Format 'yyyyMMddhhmmss').bak") } -Verifiable -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Mock -CommandName Join-Path -MockWith { [IO.Path]::Combine($databaseMembershipClass.BackupPath,"$($database.Name)_Log_$(Get-Date -Format 'yyyyMMddhhmmss').trn") } -Verifiable -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Mock -CommandName Remove-Item -Verifiable @@ -1862,7 +1862,7 @@ REVERT' Mock -CommandName Connect-SQL -MockWith { return $mockServerObject } -Verifiable -ParameterFilter { $ServerName -eq 'Server1' -and $InstanceName -eq 'MSSQLSERVER' } Mock -CommandName Connect-SQL -MockWith { return $mockServerObject } -Verifiable -ParameterFilter { $ServerName -eq 'Server1' } Mock -CommandName Connect-SQL -MockWith { return $mockServer2Object } -Verifiable -ParameterFilter { $ServerName -eq 'Server2' } - Mock -CommandName Invoke-Query -MockWith $mockResultInvokeQueryFileExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockResultInvokeQueryFileExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } Mock -CommandName Remove-SqlAvailabilityDatabase -Verifiable Mock -CommandName Test-ImpersonatePermissions -MockWith { $true } -Verifiable } @@ -1883,9 +1883,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1910,9 +1910,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1937,9 +1937,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1964,9 +1964,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -1992,9 +1992,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2019,9 +2019,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2070,9 +2070,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2100,9 +2100,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2136,9 +2136,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2167,9 +2167,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2180,7 +2180,7 @@ REVERT' } It 'Should throw the correct error when the database file path does not exist on the secondary replica' { - Mock -CommandName Invoke-Query -MockWith $mockResultInvokeQueryFileNotExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockResultInvokeQueryFileNotExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } $originalValue = $mockServer2Object.Databases['DB1'].FileGroups.Files.FileName $mockServer2Object.Databases['DB1'].FileGroups.Files.FileName = ( [IO.Path]::Combine( 'X:\', "DB1.mdf" ) ) @@ -2198,9 +2198,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2211,7 +2211,7 @@ REVERT' } It 'Should throw the correct error when the log file path does not exist on the secondary replica' { - Mock -CommandName Invoke-Query -MockWith $mockResultInvokeQueryFileNotExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockResultInvokeQueryFileNotExist -Verifiable -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } $originalValue = $mockServer2Object.Databases['DB1'].LogFiles.FileName $mockServer2Object.Databases['DB1'].LogFiles.FileName = ( [IO.Path]::Combine( 'Y:\', "DB1.ldf" ) ) @@ -2229,9 +2229,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2260,9 +2260,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2292,9 +2292,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 1 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 1 -Exactly @@ -2323,9 +2323,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2350,9 +2350,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly @@ -2382,9 +2382,9 @@ REVERT' Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 1 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server1' } Assert-MockCalled -CommandName Get-PrimaryReplicaServerObject -Scope It -Times 0 -Exactly -ParameterFilter { $AvailabilityGroup.PrimaryReplicaServerName -eq 'Server2' } Assert-MockCalled -CommandName Import-SqlDscPreferredModule -Scope It -Times 1 -Exactly - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase - Assert-MockCalled -CommandName Invoke-Query -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 2 -Exactly -ParameterFilter { $Query -like 'EXEC master.dbo.xp_fileexist *' } + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabase + Assert-MockCalled -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly -ParameterFilter $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Full_*.bak' } Assert-MockCalled -CommandName Join-Path -Scope It -Times 0 -Exactly -ParameterFilter { $ChildPath -like '*_Log_*.trn' } Assert-MockCalled -CommandName Remove-Item -Scope It -Times 0 -Exactly diff --git a/tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 b/tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 index 94b309b48..a61786929 100644 --- a/tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 +++ b/tests/Unit/DSC_SqlDatabaseUser.Tests.ps1 @@ -595,7 +595,7 @@ Describe 'SqlDatabaseUser\Test-TargetResource' -Tag 'Test' { Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { BeforeAll { - Mock -CommandName Invoke-Query + Mock -CommandName Invoke-SqlDscQuery Mock -CommandName Assert-SqlLogin Mock -CommandName Assert-DatabaseAsymmetricKey Mock -CommandName Assert-DatabaseCertificate @@ -651,7 +651,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -Exactly -Times 0 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 0 -Scope It } } @@ -689,7 +689,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -Exactly -Times 0 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 0 -Scope It } } } @@ -728,14 +728,14 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('DROP USER [{0}];' -f 'DatabaseUser1') } -Exactly -Times 1 -Scope It } - Context 'When trying to drop a database user but Invoke-Query fails' { + Context 'When trying to drop a database user but Invoke-SqlDscQuery fails' { BeforeAll { - Mock -CommandName Invoke-Query -MockWith { + Mock -CommandName Invoke-SqlDscQuery -MockWith { throw } } @@ -757,7 +757,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 1 -Scope It } } } @@ -804,7 +804,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('CREATE USER [{0}] FOR LOGIN [{1}];' -f 'DatabaseUser1', 'CONTOSO\Login1') } -Exactly -Times 1 -Scope It @@ -836,7 +836,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('CREATE USER [{0}] FOR LOGIN [{1}];' -f 'DatabaseUser1', 'CONTOSO\Login1') } -Exactly -Times 1 -Scope It @@ -862,7 +862,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('CREATE USER [{0}] WITHOUT LOGIN;' -f 'DatabaseUser1') } -Exactly -Times 1 -Scope It } @@ -884,7 +884,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('CREATE USER [{0}] FOR CERTIFICATE [{1}];' -f 'DatabaseUser1', 'Certificate1') } -Exactly -Times 1 -Scope It } @@ -906,15 +906,15 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('CREATE USER [{0}] FOR ASYMMETRIC KEY [{1}];' -f 'DatabaseUser1', 'AsymmetricKey1') } -Exactly -Times 1 -Scope It } } - Context 'When trying to create a database user but Invoke-Query fails' { + Context 'When trying to create a database user but Invoke-SqlDscQuery fails' { BeforeAll { - Mock -CommandName Invoke-Query -MockWith { + Mock -CommandName Invoke-SqlDscQuery -MockWith { throw } } @@ -937,7 +937,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 1 -Scope It } } } @@ -984,7 +984,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('ALTER USER [{0}] WITH NAME = [{1}], LOGIN = [{2}];' -f 'DatabaseUser1', 'DatabaseUser1', 'OtherLogin1') } -Exactly -Times 1 -Scope It @@ -1016,7 +1016,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('ALTER USER [{0}] WITH NAME = [{1}], LOGIN = [{2}];' -f 'DatabaseUser1', 'DatabaseUser1', 'OtherLogin1') } -Exactly -Times 1 -Scope It @@ -1026,9 +1026,9 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } } - Context 'When trying to alter the login name but Invoke-Query fails' { + Context 'When trying to alter the login name but Invoke-SqlDscQuery fails' { BeforeAll { - Mock -CommandName Invoke-Query -MockWith { + Mock -CommandName Invoke-SqlDscQuery -MockWith { throw } } @@ -1051,7 +1051,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 1 -Scope It } } } @@ -1092,11 +1092,11 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('DROP USER [{0}];' -f 'DatabaseUser1') } -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('CREATE USER [{0}] FOR ASYMMETRIC KEY [{1}];' -f 'DatabaseUser1', 'OtherAsymmetricKey1') } -Exactly -Times 1 -Scope It } @@ -1138,11 +1138,11 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('DROP USER [{0}];' -f 'DatabaseUser1') } -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('CREATE USER [{0}] FOR CERTIFICATE [{1}];' -f 'DatabaseUser1', 'OtherCertificate1') } -Exactly -Times 1 -Scope It } @@ -1184,11 +1184,11 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('DROP USER [{0}];' -f 'DatabaseUser1') } -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -ParameterFilter { + Should -Invoke -CommandName Invoke-SqlDscQuery -ParameterFilter { $Query -eq ('CREATE USER [{0}] FOR LOGIN [{1}];' -f 'DatabaseUser1', 'OtherLogin1') } -Exactly -Times 1 -Scope It } @@ -1230,7 +1230,7 @@ Describe 'DSC_SqlDatabaseUser\Set-TargetResource' -Tag 'Set' { } Should -Invoke -CommandName Get-TargetResource -Exactly -Times 1 -Scope It - Should -Invoke -CommandName Invoke-Query -Exactly -Times 0 -Scope It + Should -Invoke -CommandName Invoke-SqlDscQuery -Exactly -Times 0 -Scope It } } } diff --git a/tests/Unit/Private/ConvertTo-RedactedText.Tests.ps1 b/tests/Unit/Private/ConvertTo-RedactedText.Tests.ps1 new file mode 100644 index 000000000..f51219d7d --- /dev/null +++ b/tests/Unit/Private/ConvertTo-RedactedText.Tests.ps1 @@ -0,0 +1,112 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'because ConvertTo-SecureString is used to simplify the tests.')] +param () + +BeforeDiscovery { + try + { + if (-not (Get-Module -Name 'DscResource.Test')) + { + # Assumes dependencies has 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' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # If the dependencies has 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' + + $env:SqlServerDscCI = $true + + Import-Module -Name $script:dscModuleName + + # Loading mocked classes + Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../Stubs') -ChildPath 'SMO.cs') + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + # Unload the module being tested so that it doesn't impact any other tests. + Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' +} + +Describe 'ConvertTo-RedactedText' -Tag 'Private' { + It 'Should redact a single phrase' { + InModuleScope -Parameters $_ -ScriptBlock { + Set-StrictMode -Version 1.0 + + $result = ConvertTo-RedactedText -Text 'My secret phrase: secret123 secret456' -RedactPhrase 'secret123' + + $result | Should -Be 'My secret phrase: ******* secret456' + } + } + + It 'Should redact multiple phrases' { + InModuleScope -Parameters $_ -ScriptBlock { + Set-StrictMode -Version 1.0 + + $result = ConvertTo-RedactedText -Text 'My secret phrase: secret123 secret456' -RedactPhrase 'secret123', 'secret456' + + $result | Should -Be 'My secret phrase: ******* *******' + } + } + + It 'Should redact a multi line text' { + InModuleScope -Parameters $_ -ScriptBlock { + Set-StrictMode -Version 1.0 + + $result = ConvertTo-RedactedText -Text @' +My secret phrase: +secret123 +secret456 +'@ -RedactPhrase 'secret123', 'secret456' + + $result | Should -Be @' +My secret phrase: +******* +******* +'@ + } + } + + It 'Should redact text using optional string phrase' { + InModuleScope -Parameters $_ -ScriptBlock { + Set-StrictMode -Version 1.0 + + $result = ConvertTo-RedactedText -RedactWith '----' -Text 'My secret phrase: secret123' -RedactPhrase 'secret123' + + $result | Should -Be 'My secret phrase: ----' + } + } + + It 'Should correctly redact text that look like regular expression' { + InModuleScope -Parameters $_ -ScriptBlock { + Set-StrictMode -Version 1.0 + + # CSpell: disable-next + $result = ConvertTo-RedactedText -Text 'My secret phrase: ^s/d(ecret)123' -RedactPhrase '^s/d(ecret)123' + + $result | Should -Be 'My secret phrase: *******' + } + } +} diff --git a/tests/Unit/Public/Disconnect-SqlDscDatabaseEngine.Tests.ps1 b/tests/Unit/Public/Disconnect-SqlDscDatabaseEngine.Tests.ps1 new file mode 100644 index 000000000..4541e79f5 --- /dev/null +++ b/tests/Unit/Public/Disconnect-SqlDscDatabaseEngine.Tests.ps1 @@ -0,0 +1,129 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'because ConvertTo-SecureString is used to simplify the tests.')] +param () + +BeforeDiscovery { + try + { + if (-not (Get-Module -Name 'DscResource.Test')) + { + # Assumes dependencies has 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' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # If the dependencies has 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' + + $env:SqlServerDscCI = $true + + Import-Module -Name $script:dscModuleName + + # Loading mocked classes + Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../Stubs') -ChildPath 'SMO.cs') + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + # Unload the module being tested so that it doesn't impact any other tests. + Get-Module -Name $script:dscModuleName -All | Remove-Module -Force + + Remove-Item -Path 'env:SqlServerDscCI' +} + +Describe 'Disconnect-SqlDscDatabaseEngine' -Tag 'Public' { + It 'Should have the correct parameters in parameter set ' -ForEach @( + @{ + MockParameterSetName = '__AllParameterSets' + # cSpell: disable-next + MockExpectedParameters = '[-ServerObject] [-Force] [-WhatIf] [-Confirm] []' + } + ) { + $result = (Get-Command -Name 'Disconnect-SqlDscDatabaseEngine').ParameterSets | + Where-Object -FilterScript { + $_.Name -eq $mockParameterSetName + } | + Select-Object -Property @( + @{ + Name = 'ParameterSetName' + Expression = { $_.Name } + }, + @{ + Name = 'ParameterListAsString' + Expression = { $_.ToString() } + } + ) + + $result.ParameterSetName | Should -Be $MockParameterSetName + $result.ParameterListAsString | Should -Be $MockExpectedParameters + } + + Context 'When disconnecting using a server object' { + BeforeAll { + $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' + $mockServerObject.InstanceName = 'MockInstance' + + $mockConnectionContext = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.ServerConnection' | + Add-Member -MemberType 'ScriptMethod' -Name 'Disconnect' -Value { + $script:mockMethodDisconnectCallCount += 1 + } -PassThru -Force + + $mockServerObject.ConnectionContext = $mockConnectionContext + } + + BeforeEach { + $script:mockMethodDisconnectCallCount = 0 + } + + Context 'When using parameter Confirm with value $false' { + It 'Should call the correct mock with the expected parameters' { + { Disconnect-SqlDscDatabaseEngine -ServerObject $mockServerObject -Confirm:$false } | Should -Not -Throw + + $mockMethodDisconnectCallCount | Should -Be 1 + } + } + + Context 'When using parameter Force' { + It 'Should call the correct mock with the expected parameters' { + { Disconnect-SqlDscDatabaseEngine -ServerObject $mockServerObject -Force } | Should -Not -Throw + + $mockMethodDisconnectCallCount | Should -Be 1 + } + } + + Context 'When using parameter WhatIf' { + It 'Should call the correct mock with the expected parameters' { + { Disconnect-SqlDscDatabaseEngine -ServerObject $mockServerObject -WhatIf } | Should -Not -Throw + + $mockMethodDisconnectCallCount | Should -Be 0 + } + } + + Context 'When passing parameter ServerObject over the pipeline' { + It 'Should call the correct mock with the expected parameters' { + { $mockServerObject | Disconnect-SqlDscDatabaseEngine -Force } | Should -Not -Throw + + $mockMethodDisconnectCallCount | Should -Be 1 + } + } + } +} diff --git a/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 b/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 index f8a479c30..8638211b7 100644 --- a/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 +++ b/tests/Unit/Public/Invoke-SqlDscQuery.Tests.ps1 @@ -1,3 +1,4 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Because ConvertTo-SecureString is used to simplify the tests.')] [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] param () @@ -50,101 +51,328 @@ AfterAll { } Describe 'Invoke-SqlDscQuery' -Tag 'Public' { - Context 'When calling the command with only mandatory parameters' { + It 'Should have the correct parameters in parameter set ' -ForEach @( + @{ + MockParameterSetName = 'ByServerName' + # cSpell: disable-next + MockExpectedParameters = '-DatabaseName -Query [-ServerName ] [-InstanceName ] [-Credential ] [-LoginType ] [-PassThru] [-StatementTimeout ] [-RedactText ] [-Force] [-WhatIf] [-Confirm] []' + } + @{ + MockParameterSetName = 'ByServerObject' + # cSpell: disable-next + MockExpectedParameters = '-ServerObject -DatabaseName -Query [-PassThru] [-StatementTimeout ] [-RedactText ] [-Force] [-WhatIf] [-Confirm] []' + } + ) { + $result = (Get-Command -Name 'Invoke-SqlDscQuery').ParameterSets | + Where-Object -FilterScript { + $_.Name -eq $mockParameterSetName + } | + Select-Object -Property @( + @{ + Name = 'ParameterSetName' + Expression = { $_.Name } + }, + @{ + Name = 'ParameterListAsString' + Expression = { $_.ToString() } + } + ) + + $result.ParameterSetName | Should -Be $MockParameterSetName + $result.ParameterListAsString | Should -Be $MockExpectedParameters + } + + Context 'When executing a query that cannot return any results' { BeforeAll { - $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' + $databaseObject = New-Object -TypeName PSCustomObject | + Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'MockDatabase' -PassThru | + Add-Member -MemberType 'ScriptMethod' -Name 'ExecuteNonQuery' -Value { + param + ( + [Parameter()] + [System.String] + $sqlCommand + ) + + $script:mockMethodExecuteNonQueryCallCount += 1 + } -PassThru -Force + + $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | + Add-Member -MemberType 'NoteProperty' -Name 'Databases' -Value @{ + 'MockDatabase' = $databaseObject + } -PassThru -Force + + $mockConnectionContext = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.ServerConnection' | + Add-Member -MemberType 'NoteProperty' -Name 'StatementTimeout' -Value 100 -PassThru -Force + + $mockServerObject.ConnectionContext = $mockConnectionContext + } - Mock -CommandName Invoke-Query + BeforeEach { + $script:mockMethodExecuteNonQueryCallCount = 0 + $script:mockMethodExecuteWithResultsCallCount = 0 } - It 'Should execute the query without throwing and without returning any result' { - $result = Invoke-SqlDscQuery -ServerObject $mockServerObject -DatabaseName 'master' -Query 'select name from sys.databases' + Context 'When calling using an existing server object' { + Context 'When calling the command with only mandatory parameters' { + Context 'When using parameter Confirm with value $false' { + It 'Should execute the query without throwing and without returning any result' { + $result = Invoke-SqlDscQuery -ServerObject $mockServerObject -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Confirm:$false + + $result | Should -BeNullOrEmpty + + $mockMethodExecuteNonQueryCallCount | Should -Be 1 + } + } + + Context 'When using parameter Force' { + It 'Should execute the query without throwing and without returning any result' { + $result = Invoke-SqlDscQuery -ServerObject $mockServerObject -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Force + + $result | Should -BeNullOrEmpty + + $mockMethodExecuteNonQueryCallCount | Should -Be 1 + } + } + + Context 'When using parameter WhatId' { + It 'Should execute the query without throwing and without returning any result' { + $result = Invoke-SqlDscQuery -ServerObject $mockServerObject -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -WhatIf + + $result | Should -BeNullOrEmpty + + $mockMethodExecuteNonQueryCallCount | Should -Be 0 + } + } - $result | Should -BeNullOrEmpty + Context 'When passing parameter ServerObject over the pipeline' { + It 'Should execute the query without throwing and without returning any result' { + $result = $mockServerObject | Invoke-SqlDscQuery -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Force - Should -Invoke -CommandName Invoke-Query -ParameterFilter { - $PesterBoundParameters.Keys -contains 'SqlServerObject' -and - $PesterBoundParameters.Keys -contains 'Database' -and - $PesterBoundParameters.Keys -contains 'Query' - } -Exactly -Times 1 -Scope It + $result | Should -BeNullOrEmpty + + $mockMethodExecuteNonQueryCallCount | Should -Be 1 + } + } + } + + Context 'When calling the command with optional parameter StatementTimeout' { + It 'Should execute the query without throwing and without returning any result' { + $result = Invoke-SqlDscQuery -StatementTimeout 900 -ServerObject $mockServerObject -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Force + + $result | Should -BeNullOrEmpty + + $mockMethodExecuteNonQueryCallCount | Should -Be 1 + } + } + + Context 'When calling the command with optional parameter RedactText' { + It 'Should execute the query without throwing and without returning any result' { + $result = Invoke-SqlDscQuery -RedactText @('MyString') -ServerObject $mockServerObject -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Force + + $result | Should -BeNullOrEmpty + + $mockMethodExecuteNonQueryCallCount | Should -Be 1 + } + } } - Context 'When passing ServerObject over the pipeline' { + Context 'When calling by using server name and instance name' { + BeforeAll { + $mockSqlCredentialUserName = 'TestUserName12345' + $mockSqlCredentialPassword = 'StrongOne7.' + $mockSqlCredentialSecurePassword = ConvertTo-SecureString -String $mockSqlCredentialPassword -AsPlainText -Force + $mockSqlCredential = [System.Management.Automation.PSCredential]::new($mockSqlCredentialUserName, $mockSqlCredentialSecurePassword) + + Mock -CommandName Disconnect-SqlDscDatabaseEngine + Mock -CommandName Connect-SqlDscDatabaseEngine -MockWith { + return $mockServerObject + } + } + It 'Should execute the query without throwing and without returning any result' { - $result = $mockServerObject | Invoke-SqlDscQuery -DatabaseName 'master' -Query 'select name from sys.databases' + $result = Invoke-SqlDscQuery -ServerName 'localhost' -InstanceName 'INSTANCE' -LoginType 'WindowsUser' -Credential $mockSqlCredential -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Force $result | Should -BeNullOrEmpty - Should -Invoke -CommandName Invoke-Query -ParameterFilter { - $PesterBoundParameters.Keys -contains 'SqlServerObject' -and - $PesterBoundParameters.Keys -contains 'Database' -and - $PesterBoundParameters.Keys -contains 'Query' - } -Exactly -Times 1 -Scope It + $mockMethodExecuteNonQueryCallCount | Should -Be 1 + + Should -Invoke -CommandName Connect-SqlDscDatabaseEngine -Exactly -Times 1 -Scope It + Should -Invoke -CommandName Disconnect-SqlDscDatabaseEngine -Exactly -Times 1 -Scope It } } } - Context 'When calling the command with optional parameter StatementTimeout' { + Context 'When executing a query that should return results' { BeforeAll { - $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' + $databaseObject = New-Object -TypeName PSCustomObject | + Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'MockDatabase' -PassThru | + Add-Member -MemberType ScriptMethod -Name 'ExecuteWithResults' -Value { + param + ( + [Parameter()] + [System.String] + $sqlCommand + ) + + $script:mockMethodExecuteWithResultsCallCount += 1 + + return New-Object -TypeName 'System.Data.DataSet' + } -PassThru -Force + + $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | + Add-Member -MemberType 'NoteProperty' -Name 'Databases' -Value @{ + 'MockDatabase' = $databaseObject + } -PassThru -Force + + $mockConnectionContext = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.ServerConnection' | + Add-Member -MemberType 'NoteProperty' -Name 'StatementTimeout' -Value 100 -PassThru -Force + + $mockServerObject.ConnectionContext = $mockConnectionContext + } - Mock -CommandName Invoke-Query + BeforeEach { + $script:mockMethodExecuteWithResultsCallCount = 0 } - It 'Should execute the query without throwing and without returning any result' { - $result = Invoke-SqlDscQuery -StatementTimeout 900 -ServerObject $mockServerObject -DatabaseName 'master' -Query 'select name from sys.databases' + Context 'When calling using an existing server object' { + Context 'When calling the command with optional parameter PassThru' { + Context 'When using parameter Confirm with value $false' { + It 'Should execute the query without throwing and return the expected data set' { + $result = Invoke-SqlDscQuery -PassThru -ServerObject $mockServerObject -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Confirm:$false + + $result | Should -HaveType [System.Data.DataSet] + + $mockMethodExecuteWithResultsCallCount | Should -Be 1 + } + } + + Context 'When using parameter Force' { + It 'Should execute the query without throwing and return the expected data set' { + $result = Invoke-SqlDscQuery -PassThru -ServerObject $mockServerObject -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Force + + $result | Should -HaveType [System.Data.DataSet] + + $mockMethodExecuteWithResultsCallCount | Should -Be 1 + } + } + + Context 'When using parameter WhatIf' { + It 'Should execute the query without throwing and return the expected data set' { + $result = Invoke-SqlDscQuery -PassThru -ServerObject $mockServerObject -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -WhatIf - $result | Should -BeNullOrEmpty + $result | Should -BeNullOrEmpty - Should -Invoke -CommandName Invoke-Query -ParameterFilter { - $PesterBoundParameters.Keys -contains 'SqlServerObject' -and - $PesterBoundParameters.Keys -contains 'Database' -and - $PesterBoundParameters.Keys -contains 'Query' -and - $PesterBoundParameters.Keys -contains 'StatementTimeout' - } -Exactly -Times 1 -Scope It + $mockMethodExecuteWithResultsCallCount | Should -Be 0 + } + } + + Context 'When passing parameter ServerObject over the pipeline' { + It 'Should execute the query without throwing and return the expected data set' { + $result = $mockServerObject | Invoke-SqlDscQuery -PassThru -DatabaseName 'MockDatabase' -Query 'select name from sys.databases' -Force + + $result | Should -HaveType [System.Data.DataSet] + + $mockMethodExecuteWithResultsCallCount | Should -Be 1 + } + } + } } } - Context 'When calling the command with optional parameter RedactText' { + Context 'When an exception is thrown' { BeforeAll { - $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' + $databaseObject = New-Object -TypeName PSCustomObject | + Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'MockDatabase' -PassThru | + Add-Member -MemberType ScriptMethod -Name 'ExecuteWithResults' -Value { + $script:mockMethodExecuteWithResultsCallCount += 1 + + throw 'Mocked error' + } -PassThru | + Add-Member -MemberType 'ScriptMethod' -Name 'ExecuteNonQuery' -Value { + $script:mockMethodExecuteNonQueryCallCount += 1 + } -PassThru -Force + + $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | + Add-Member -MemberType 'NoteProperty' -Name 'Databases' -Value @{ + 'MockDatabase' = $databaseObject + } -PassThru -Force + + $mockConnectionContext = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.ServerConnection' | + Add-Member -MemberType 'NoteProperty' -Name 'StatementTimeout' -Value 100 -PassThru -Force + + $mockServerObject.ConnectionContext = $mockConnectionContext + } - Mock -CommandName Invoke-Query + BeforeEach { + $script:mockMethodExecuteWithResultsCallCount = 0 + $script:mockMethodExecuteNonQueryCallCount = 0 } - It 'Should execute the query without throwing and without returning any result' { - $result = Invoke-SqlDscQuery -RedactText @('MyString') -ServerObject $mockServerObject -DatabaseName 'master' -Query 'select name from sys.databases' + Context 'When ErrorAction is set to Stop' { + BeforeAll { + $mockInvokeSqlDscQueryParameters = @{ + PassThru = $true + ServerObject = $mockServerObject + DatabaseName = 'MockDatabase' + Query = 'select name from sys.databases' + Force = $true + ErrorAction = 'Stop' + } + } - $result | Should -BeNullOrEmpty + Context 'When executing a query that should return results' { + It 'Should throw the correct error' { + { + Invoke-SqlDscQuery @mockInvokeSqlDscQueryParameters + } | Should -Throw -ExpectedMessage '*Mocked error*' - Should -Invoke -CommandName Invoke-Query -ParameterFilter { - $PesterBoundParameters.Keys -contains 'SqlServerObject' -and - $PesterBoundParameters.Keys -contains 'Database' -and - $PesterBoundParameters.Keys -contains 'Query' -and - $PesterBoundParameters.Keys -contains 'RedactText' - } -Exactly -Times 1 -Scope It - } - } + $mockMethodExecuteWithResultsCallCount | Should -Be 1 + } + } - Context 'When calling the command with optional parameter PassThru' { - BeforeAll { - $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' + Context 'When executing a query that cannot return any results' { + It 'Should throw the correct error' { + { + Invoke-SqlDscQuery @mockInvokeSqlDscQueryParameters + } | Should -Throw -ExpectedMessage '*Mocked error*' - # Actual testing that Invoke-Query returns values is done in the unit tests for Invoke-Query. - Mock -CommandName Invoke-Query + $mockMethodExecuteWithResultsCallCount | Should -Be 1 + } + } } - It 'Should execute the query without throwing and without returning any result' { - $result = Invoke-SqlDscQuery -PassThru -ServerObject $mockServerObject -DatabaseName 'master' -Query 'select name from sys.databases' + Context 'When ErrorAction is set to Ignore or SilentlyContinue' { + BeforeAll { + $mockInvokeSqlDscQueryParameters = @{ + PassThru = $true + ServerObject = $mockServerObject + DatabaseName = 'MockDatabase' + Query = 'select name from sys.databases' + Force = $true + ErrorAction = 'Ignore' + } + } + + Context 'When executing a query that should return results' { + It 'Should not throw an exception and does not return any result' { + $result = Invoke-SqlDscQuery @mockInvokeSqlDscQueryParameters + + $result | Should -BeNullOrEmpty + + $mockMethodExecuteWithResultsCallCount | Should -Be 1 + } + } + + Context 'When executing a query that cannot return any results' { + It 'Should not throw an exception and does not return any result' { + $result = Invoke-SqlDscQuery @mockInvokeSqlDscQueryParameters - $result | Should -BeNullOrEmpty + $result | Should -BeNullOrEmpty - Should -Invoke -CommandName Invoke-Query -ParameterFilter { - $PesterBoundParameters.Keys -contains 'SqlServerObject' -and - $PesterBoundParameters.Keys -contains 'Database' -and - $PesterBoundParameters.Keys -contains 'Query' -and - $PesterBoundParameters.Keys -contains 'WithResults' - } -Exactly -Times 1 -Scope It + $mockMethodExecuteWithResultsCallCount | Should -Be 1 + } + } } } } diff --git a/tests/Unit/SqlServerDsc.Common.Tests.ps1 b/tests/Unit/SqlServerDsc.Common.Tests.ps1 index 7eb4d85c7..9da5692b7 100644 --- a/tests/Unit/SqlServerDsc.Common.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.Common.Tests.ps1 @@ -1856,321 +1856,6 @@ Describe 'SqlServerDsc.Common\Connect-SQLAnalysis' -Tag 'ConnectSQLAnalysis' { } } -Describe 'SqlServerDsc.Common\Invoke-Query' -Tag 'InvokeQuery' { - BeforeAll { - $mockExpectedQuery = '' - - $mockSqlCredentialUserName = 'TestUserName12345' - $mockSqlCredentialPassword = 'StrongOne7.' - $mockSqlCredentialSecurePassword = ConvertTo-SecureString -String $mockSqlCredentialPassword -AsPlainText -Force - $mockSqlCredential = New-Object -TypeName PSCredential -ArgumentList ($mockSqlCredentialUserName, $mockSqlCredentialSecurePassword) - - $masterDatabaseObject = New-Object -TypeName PSObject - $masterDatabaseObject | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'master' - $masterDatabaseObject | Add-Member -MemberType ScriptMethod -Name 'ExecuteNonQuery' -Value { - param - ( - [Parameter()] - [System.String] - $sqlCommand - ) - - if ( $sqlCommand -ne $mockExpectedQuery ) - { - throw - } - } - - $masterDatabaseObject | Add-Member -MemberType ScriptMethod -Name 'ExecuteWithResults' -Value { - param - ( - [Parameter()] - [System.String] - $sqlCommand - ) - - if ( $sqlCommand -ne $mockExpectedQuery ) - { - throw - } - - return New-Object -TypeName System.Data.DataSet - } - - $databasesObject = New-Object -TypeName PSObject - $databasesObject | Add-Member -MemberType NoteProperty -Name 'Databases' -Value @{ - 'master' = $masterDatabaseObject - } - - $mockSMOServer = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' - $mockSMOServer | Add-Member -MemberType NoteProperty -Name 'Databases' -Value @{ - 'master' = $masterDatabaseObject - } -Force - - $mockConnectSql = { - return @($databasesObject) - } - - $queryParameters = @{ - ServerName = 'Server1' - InstanceName = 'MSSQLSERVER' - Database = 'master' - Query = '' - DatabaseCredential = $mockSqlCredential - } - - $queryParametersWithSMO = @{ - Query = '' - SqlServerObject = $mockSMOServer - Database = 'master' - } - } - - BeforeEach { - Mock -CommandName Connect-SQL -MockWith $mockConnectSql - } - - Context 'When executing a query with no results' { - AfterEach { - Should -Invoke -CommandName Connect-SQL -Scope It -Times 1 -Exactly - } - - It 'Should execute the query silently' { - $queryParameters.Query = "EXEC sp_configure 'show advanced option', '1'" - $mockExpectedQuery = $queryParameters.Query.Clone() - - { Invoke-Query @queryParameters } | Should -Not -Throw - - Should -Invoke -CommandName Connect-SQL -ParameterFilter { - <# - Should not be called with a login type. - - Due to issue https://github.com/pester/Pester/issues/1542 - we cannot use `$PSBoundParameters.ContainsKey('LoginType') -eq $false`. - #> - $null -eq $LoginType - } -Scope It -Times 1 -Exactly - } - - It 'Should throw the correct error, ExecuteNonQueryFailed, when executing the query fails' { - $queryParameters.Query = 'BadQuery' - - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.ExecuteNonQueryFailed - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f $queryParameters.Database - ) - - { Invoke-Query @queryParameters } | Should -Throw -ExpectedMessage ($mockErrorRecord.Exception.Message + '*') - } - - Context 'When text should be redacted' { - BeforeAll { - Mock -CommandName Write-Verbose -ParameterFilter { - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.ExecuteNonQuery - } - - $Message -eq ( - $mockLocalizedString -f - "select * from MyTable where password = '*******' and password = '*******'" - ) - } -MockWith { - <# - MUST return another message than the parameter filter - is looking for, otherwise we get into a endless loop. - We returning the to show in the output how the verbose - message was redacted. - #> - Write-Verbose -Message ('MOCK OUTPUT: {0}' -f $Message) -Verbose - } - } - - It 'Should execute the query silently and redact text in the verbose output' { - $queryParameters.Query = "select * from MyTable where password = 'Pa\ssw0rd1' and password = 'secret passphrase'" - $mockExpectedQuery = $queryParameters.Query.Clone() - - # The `Secret PassPhrase` is using the casing like this to test case-insensitive replace. - { Invoke-Query @queryParameters -RedactText @('Pa\sSw0rd1', 'Secret PassPhrase') } | Should -Not -Throw - } - } - } - - Context 'When executing a query with no results using Windows impersonation' { - It 'Should execute the query silently' { - $testParameters = $queryParameters.Clone() - $testParameters.LoginType = 'WindowsUser' - $testParameters.Query = "EXEC sp_configure 'show advanced option', '1'" - $mockExpectedQuery = $testParameters.Query.Clone() - - { Invoke-Query @testParameters } | Should -Not -Throw - - Should -Invoke -CommandName Connect-SQL -ParameterFilter { - $LoginType -eq 'WindowsUser' - } -Scope It -Times 1 -Exactly - } - } - - Context 'when executing a query with no results using SQL impersonation' { - It 'Should execute the query silently' { - $testParameters = $queryParameters.Clone() - $testParameters.LoginType = 'SqlLogin' - $testParameters.Query = "EXEC sp_configure 'show advanced option', '1'" - $mockExpectedQuery = $testParameters.Query.Clone() - - { Invoke-Query @testParameters } | Should -Not -Throw - - Should -Invoke -CommandName Connect-SQL -ParameterFilter { - $LoginType -eq 'SqlLogin' - } -Scope It -Times 1 -Exactly - } - } - - Context 'when executing a query with results' { - It 'Should execute the query and return a result set' { - $queryParameters.Query = 'SELECT name FROM sys.databases' - $mockExpectedQuery = $queryParameters.Query.Clone() - - Invoke-Query @queryParameters -WithResults | Should -Not -BeNullOrEmpty - - Should -Invoke -CommandName Connect-SQL -Scope It -Times 1 -Exactly - } - - It 'Should throw the correct error, ExecuteQueryWithResultsFailed, when executing the query fails' { - $queryParameters.Query = 'BadQuery' - - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.ExecuteQueryWithResultsFailed - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f $queryParameters.Database - ) - - { Invoke-Query @queryParameters -WithResults } | Should -Throw -ExpectedMessage ($mockErrorRecord.Exception.Message + '*') - - Should -Invoke -CommandName Connect-SQL -Scope It -Times 1 -Exactly - } - - Context 'When text should be redacted' { - BeforeAll { - Mock -CommandName Write-Verbose -ParameterFilter { - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.ExecuteQueryWithResults - } - - $Message -eq ( - $mockLocalizedString -f - "select * from MyTable where password = '*******' and password = '*******'" - ) - } -MockWith { - <# - MUST return another message than the parameter filter - is looking for, otherwise we get into a endless loop. - We returning the to show in the output how the verbose - message was redacted. - #> - Write-Verbose -Message ('MOCK OUTPUT: {0}' -f $Message) -Verbose - } - } - - It 'Should execute the query silently and redact text in the verbose output' { - $queryParameters.Query = "select * from MyTable where password = 'Pa\ssw0rd1' and password = 'secret passphrase'" - $mockExpectedQuery = $queryParameters.Query.Clone() - - # The `Secret PassPhrase` is using the casing like this to test case-insensitive replace. - { Invoke-Query @queryParameters -RedactText @('Pa\sSw0rd1', 'Secret PassPhrase') -WithResults } | Should -Not -Throw - } - } - } - - Context 'When passing in an SMO Server Object' { - Context 'Execute a query with no results' { - It 'Should execute the query silently' { - $queryParametersWithSMO.Query = "EXEC sp_configure 'show advanced option', '1'" - $mockExpectedQuery = $queryParametersWithSMO.Query.Clone() - - { Invoke-Query @queryParametersWithSMO } | Should -Not -Throw - - Should -Invoke -CommandName Connect-SQL -Scope It -Times 0 -Exactly - } - - It 'Should throw the correct error, ExecuteNonQueryFailed, when executing the query fails' { - $queryParametersWithSMO.Query = 'BadQuery' - - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.ExecuteNonQueryFailed - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f $queryParameters.Database - ) - - { Invoke-Query @queryParametersWithSMO } | Should -Throw -ExpectedMessage ($mockErrorRecord.Exception.Message + '*') - - Should -Invoke -CommandName Connect-SQL -Scope It -Times 0 -Exactly - } - } - - Context 'When executing a query with results' { - It 'Should execute the query and return a result set' { - $queryParametersWithSMO.Query = 'SELECT name FROM sys.databases' - $mockExpectedQuery = $queryParametersWithSMO.Query.Clone() - - Invoke-Query @queryParametersWithSMO -WithResults | Should -Not -BeNullOrEmpty - - Should -Invoke -CommandName Connect-SQL -Scope It -Times 0 -Exactly - } - - It 'Should throw the correct error, ExecuteQueryWithResultsFailed, when executing the query fails' { - $queryParametersWithSMO.Query = 'BadQuery' - - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.ExecuteQueryWithResultsFailed - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f $queryParameters.Database - ) - - { Invoke-Query @queryParametersWithSMO -WithResults } | Should -Throw -ExpectedMessage ($mockErrorRecord.Exception.Message + '*') - - Should -Invoke -CommandName Connect-SQL -Scope It -Times 0 -Exactly - } - } - - Context 'When executing a query with piped SMO server object' { - It 'Should execute the query and return a result set' { - $mockQuery = 'SELECT name FROM sys.databases' - $mockExpectedQuery = $mockQuery - - $mockSMOServer | Invoke-Query -Query $mockQuery -Database master -WithResults | - Should -Not -BeNullOrEmpty - - Should -Invoke -CommandName Connect-SQL -Scope It -Times 0 -Exactly - } - - It 'Should throw the correct error, ExecuteQueryWithResultsFailed, when executing the query fails' { - $mockQuery = 'BadQuery' - - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.ExecuteQueryWithResultsFailed - } - - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f $queryParameters.Database - ) - - { $mockSMOServer | Invoke-Query -Query $mockQuery -Database master -WithResults } | Should -Throw -ExpectedMessage ($mockErrorRecord.Exception.Message + '*') - - Should -Invoke -CommandName Connect-SQL -Scope It -Times 0 -Exactly - } - } - } -} - Describe 'SqlServerDsc.Common\Update-AvailabilityGroupReplica' -Tag 'UpdateAvailabilityGroupReplica' { Context 'When the Availability Group Replica is altered' { It 'Should silently alter the Availability Group Replica' { @@ -2251,7 +1936,7 @@ Describe 'SqlServerDsc.Common\Test-LoginEffectivePermissions' -Tag 'TestLoginEff } BeforeEach { - Mock -CommandName Invoke-Query -MockWith $mockInvokeQueryPermissionsResult + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockInvokeQueryPermissionsResult } Context 'When all of the permissions are present' { @@ -2261,7 +1946,7 @@ Describe 'SqlServerDsc.Common\Test-LoginEffectivePermissions' -Tag 'TestLoginEff Test-LoginEffectivePermissions @testLoginEffectiveServerPermissionsParams | Should -Be $true - Should -Invoke -CommandName Invoke-Query -Scope It -Times 1 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 1 -Exactly } It 'Should return $true when the desired login permissions are present' { @@ -2270,7 +1955,7 @@ Describe 'SqlServerDsc.Common\Test-LoginEffectivePermissions' -Tag 'TestLoginEff Test-LoginEffectivePermissions @testLoginEffectiveLoginPermissionsParams | Should -Be $true - Should -Invoke -CommandName Invoke-Query -Scope It -Times 1 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 1 -Exactly } } @@ -2281,7 +1966,7 @@ Describe 'SqlServerDsc.Common\Test-LoginEffectivePermissions' -Tag 'TestLoginEff Test-LoginEffectivePermissions @testLoginEffectiveServerPermissionsParams | Should -Be $false - Should -Invoke -CommandName Invoke-Query -Scope It -Times 1 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 1 -Exactly } It 'Should return $false when the specified login has no server permissions assigned' { @@ -2290,7 +1975,7 @@ Describe 'SqlServerDsc.Common\Test-LoginEffectivePermissions' -Tag 'TestLoginEff Test-LoginEffectivePermissions @testLoginEffectiveServerPermissionsParams | Should -Be $false - Should -Invoke -CommandName Invoke-Query -Scope It -Times 1 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 1 -Exactly } It 'Should return $false when the desired login permissions are not present' { @@ -2299,7 +1984,7 @@ Describe 'SqlServerDsc.Common\Test-LoginEffectivePermissions' -Tag 'TestLoginEff Test-LoginEffectivePermissions @testLoginEffectiveLoginPermissionsParams | Should -Be $false - Should -Invoke -CommandName Invoke-Query -Scope It -Times 1 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 1 -Exactly } It 'Should return $false when the specified login has no login permissions assigned' { @@ -2308,7 +1993,7 @@ Describe 'SqlServerDsc.Common\Test-LoginEffectivePermissions' -Tag 'TestLoginEff Test-LoginEffectivePermissions @testLoginEffectiveLoginPermissionsParams | Should -Be $false - Should -Invoke -CommandName Invoke-Query -Scope It -Times 1 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 1 -Exactly } } } @@ -2509,7 +2194,7 @@ Describe 'SqlServerDsc.Common\Test-AvailabilityReplicaSeedingModeAutomatic' -Tag Context 'When the replica seeding mode is manual' { BeforeEach { Mock -CommandName Connect-SQL -MockWith $mockConnectSql - Mock -CommandName Invoke-Query -MockWith $mockInvokeQuery + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockInvokeQuery } It 'Should return $false when the instance version is <_>' -ForEach @(11, 12) { @@ -2518,7 +2203,7 @@ Describe 'SqlServerDsc.Common\Test-AvailabilityReplicaSeedingModeAutomatic' -Tag Test-AvailabilityReplicaSeedingModeAutomatic @testAvailabilityReplicaSeedingModeAutomaticParams | Should -Be $false Should -Invoke -CommandName Connect-SQL -Scope It -Times 1 -Exactly - Should -Invoke -CommandName Invoke-Query -Scope It -Times 0 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 0 -Exactly } # Test SQL 2016 and later where Seeding Mode is supported. @@ -2528,14 +2213,14 @@ Describe 'SqlServerDsc.Common\Test-AvailabilityReplicaSeedingModeAutomatic' -Tag Test-AvailabilityReplicaSeedingModeAutomatic @testAvailabilityReplicaSeedingModeAutomaticParams | Should -Be $false Should -Invoke -CommandName Connect-SQL -Scope It -Times 1 -Exactly - Should -Invoke -CommandName Invoke-Query -Scope It -Times 1 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 1 -Exactly } } Context 'When the replica seeding mode is automatic' { BeforeEach { Mock -CommandName Connect-SQL -MockWith $mockConnectSql - Mock -CommandName Invoke-Query -MockWith $mockInvokeQuery + Mock -CommandName Invoke-SqlDscQuery -MockWith $mockInvokeQuery } # Test SQL 2016 and later where Seeding Mode is supported. @@ -2546,7 +2231,7 @@ Describe 'SqlServerDsc.Common\Test-AvailabilityReplicaSeedingModeAutomatic' -Tag Test-AvailabilityReplicaSeedingModeAutomatic @testAvailabilityReplicaSeedingModeAutomaticParams | Should -Be $true Should -Invoke -CommandName Connect-SQL -Scope It -Times 1 -Exactly - Should -Invoke -CommandName Invoke-Query -Scope It -Times 1 -Exactly + Should -Invoke -CommandName Invoke-SqlDscQuery -Scope It -Times 1 -Exactly } } }