From 979800d7dbc305d70d44df0e511413e45497c654 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 24 Jan 2023 12:54:51 +0100 Subject: [PATCH] SqlServerDsc: `Connect-Sql` now handles `ErrorAction` correctly (#1839) - SqlServerDsc.Common - `Connect-SQL` - Was updated to handle both `-ErrorAction 'Stop'` and `-ErrorAction 'SilentlyContinue'` when passed to the command ([issue #1837]). --- CHANGELOG.md | 3 + source/Classes/011.SqlResourceBase.ps1 | 1 + source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 | 4 +- .../DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 | 18 +- .../DSC_SqlAGListener/DSC_SqlAGListener.psm1 | 8 +- .../DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 | 6 +- .../DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 | 4 +- .../DSC_SqlAgentFailsafe.psm1 | 4 +- .../DSC_SqlAgentOperator.psm1 | 4 +- .../DSC_SqlAlwaysOnService.psm1 | 2 +- .../DSC_SqlConfiguration.psm1 | 4 +- .../DSC_SqlDatabase/DSC_SqlDatabase.psm1 | 4 +- .../DSC_SqlDatabaseDefaultLocation.psm1 | 4 +- .../DSC_SqlDatabaseMail.psm1 | 4 +- .../DSC_SqlDatabaseObjectPermission.psm1 | 2 +- .../DSC_SqlDatabaseRole.psm1 | 4 +- .../DSC_SqlDatabaseUser.psm1 | 8 +- .../DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 | 4 +- .../DSC_SqlEndpointPermission.psm1 | 4 +- .../DSC_SqlLogin/DSC_SqlLogin.psm1 | 6 +- .../DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 | 4 +- .../DSC_SqlMemory/DSC_SqlMemory.psm1 | 4 +- .../DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 | 4 +- .../DSC_SqlSetup/DSC_SqlSetup.psm1 | 6 +- .../DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 | 2 +- .../SqlServerDsc.Common.psm1 | 27 ++- tests/Unit/SqlServerDsc.Common.Tests.ps1 | 155 ++++++++++++++---- 27 files changed, 207 insertions(+), 93 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c7717954..0f2dc98b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -163,6 +163,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 only be used together with the module _SqlServer_ v22.x (minimum v22.0.49-preview). The parameter will be ignored if an older major versions of the module _SqlServer_ is used. + - `Connect-SQL` + - Was updated to handle both `-ErrorAction 'Stop'` and `-ErrorAction 'SilentlyContinue'` + when passed to the command ([issue #1837](https://github.com/dsccommunity/SqlServerDsc/issues/1837)). ### Fixed diff --git a/source/Classes/011.SqlResourceBase.ps1 b/source/Classes/011.SqlResourceBase.ps1 index 51df4a359..369757d46 100644 --- a/source/Classes/011.SqlResourceBase.ps1 +++ b/source/Classes/011.SqlResourceBase.ps1 @@ -67,6 +67,7 @@ class SqlResourceBase : ResourceBase $connectSqlDscDatabaseEngineParameters = @{ ServerName = $this.ServerName InstanceName = $this.InstanceName + ErrorAction = 'Stop' } if ($this.Credential) diff --git a/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 b/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 index be287f785..410e614e8 100644 --- a/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 +++ b/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 @@ -43,7 +43,7 @@ function Get-TargetResource ) # Connect to the instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Define current version for check compatibility $sqlMajorVersion = $serverObject.Version.Major @@ -244,7 +244,7 @@ function Set-TargetResource ) # Connect to the instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Determine if HADR is enabled on the instance. If not, throw an error if ( -not $serverObject.IsHadrEnabled ) diff --git a/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 b/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 index fbbaa8a98..e3afd6b2f 100644 --- a/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 +++ b/source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1 @@ -72,7 +72,7 @@ function Get-TargetResource } # Connect to the instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Is this node actively hosting the SQL instance? $currentConfiguration.IsActiveNode = Test-ActiveNode -ServerObject $serverObject @@ -213,7 +213,7 @@ function Set-TargetResource Import-SQLPSModule # Connect to the defined instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -StatementTimeout $StatementTimeout + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -StatementTimeout $StatementTimeout -ErrorAction 'Stop' # Get the Availability Group $availabilityGroup = $serverObject.AvailabilityGroups[$AvailabilityGroupName] @@ -264,7 +264,7 @@ function Set-TargetResource foreach ( $availabilityGroupReplica in $secondaryReplicas ) { - $currentAvailabilityGroupReplicaServerObject = Connect-SQL -ServerName $availabilityGroupReplica.Name + $currentAvailabilityGroupReplicaServerObject = Connect-SQL -ServerName $availabilityGroupReplica.Name -ErrorAction 'Stop' $impersonatePermissionsStatus.Add( $availabilityGroupReplica.Name, ( Test-ImpersonatePermissions -ServerObject $currentAvailabilityGroupReplicaServerObject -Securable $databaseObject.Owner ) @@ -328,7 +328,7 @@ function Set-TargetResource foreach ( $availabilityGroupReplica in $secondaryReplicas ) { $connectSqlParameters = Split-FullSqlInstanceName -FullSqlInstanceName $availabilityGroupReplica.Name - $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters + $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters -ErrorAction 'Stop' $availabilityReplicaFilestreamLevel.Add($availabilityGroupReplica.Name, $currentAvailabilityGroupReplicaServerObject.FilestreamLevel) } @@ -349,7 +349,7 @@ function Set-TargetResource foreach ( $availabilityGroupReplica in $secondaryReplicas ) { $connectSqlParameters = Split-FullSqlInstanceName -FullSqlInstanceName $availabilityGroupReplica.Name - $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters + $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters -ErrorAction 'Stop' $availabilityReplicaContainmentEnabled.Add($availabilityGroupReplica.Name, $currentAvailabilityGroupReplicaServerObject.Configuration.ContainmentEnabled.ConfigValue) } @@ -373,7 +373,7 @@ function Set-TargetResource foreach ( $availabilityGroupReplica in $secondaryReplicas ) { $connectSqlParameters = Split-FullSqlInstanceName -FullSqlInstanceName $availabilityGroupReplica.Name - $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters + $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters -ErrorAction 'Stop' $missingDirectories = @() foreach ( $databaseFileDirectory in $databaseFileDirectories ) @@ -411,7 +411,7 @@ function Set-TargetResource foreach ( $availabilityGroupReplica in $secondaryReplicas ) { $connectSqlParameters = Split-FullSqlInstanceName -FullSqlInstanceName $availabilityGroupReplica.Name - $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters + $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters -ErrorAction 'Stop' [System.Array] $installedCertificateThumbprints = $currentAvailabilityGroupReplicaServerObject.Databases['master'].Certificates | ForEach-Object -Process { [System.BitConverter]::ToString($_.Thumbprint) } @@ -594,7 +594,7 @@ function Set-TargetResource { # Connect to the replica $connectSqlParameters = Split-FullSqlInstanceName -FullSqlInstanceName $availabilityGroupReplica.Name - $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters + $currentAvailabilityGroupReplicaServerObject = Connect-SQL @connectSqlParameters -ErrorAction 'Stop' $currentReplicaAvailabilityGroupObject = $currentAvailabilityGroupReplicaServerObject.AvailabilityGroups[$AvailabilityGroupName] if ( $availabilityGroupReplica.SeedingMode -eq 'MANUAL') @@ -801,7 +801,7 @@ function Test-TargetResource } # Connect to the defined instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Get the Availability Group if it exists if ( -not [System.String]::IsNullOrEmpty($currentConfiguration.AvailabilityGroupName) ) diff --git a/source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 b/source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 index a5fec48d7..bfc99b739 100644 --- a/source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 +++ b/source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 @@ -195,7 +195,7 @@ function Set-TargetResource $script:localizedData.CreateAvailabilityGroupListener -f $Name, $AvailabilityGroup, $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $availabilityGroupObject = $sqlServerObject.AvailabilityGroups[$AvailabilityGroup] if ($availabilityGroupObject) @@ -255,7 +255,7 @@ function Set-TargetResource $script:localizedData.DropAvailabilityGroupListener -f $Name, $AvailabilityGroup, $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $availabilityGroupObject = $sqlServerObject.AvailabilityGroups[$AvailabilityGroup] if ($availabilityGroupObject) @@ -312,7 +312,7 @@ function Set-TargetResource New-InvalidOperationException -Message $errorMessage } - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $availabilityGroupObject = $sqlServerObject.AvailabilityGroups[$AvailabilityGroup] if ($availabilityGroupObject) @@ -559,7 +559,7 @@ function Get-SQLAlwaysOnAvailabilityGroupListener $script:localizedData.DebugConnectingAvailabilityGroup -f $Name, [System.Security.Principal.WindowsIdentity]::GetCurrent().Name ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $availabilityGroupObject = $sqlServerObject.AvailabilityGroups[$AvailabilityGroup] if ($availabilityGroupObject) diff --git a/source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 b/source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 index 0d54488ec..17f3bd7a6 100644 --- a/source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 +++ b/source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 @@ -50,7 +50,7 @@ function Get-TargetResource ) # Connect to the instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Is this node actively hosting the SQL instance? $isActiveNode = Test-ActiveNode -ServerObject $serverObject @@ -241,7 +241,7 @@ function Set-TargetResource Import-SQLPSModule # Connect to the instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Determine if HADR is enabled on the instance. If not, throw an error if ( -not $serverObject.IsHadrEnabled ) @@ -403,7 +403,7 @@ function Set-TargetResource else { # Connect to the instance that is supposed to house the primary replica - $primaryReplicaServerObject = Connect-SQL -ServerName $PrimaryReplicaServerName -InstanceName $PrimaryReplicaInstanceName + $primaryReplicaServerObject = Connect-SQL -ServerName $PrimaryReplicaServerName -InstanceName $PrimaryReplicaInstanceName -ErrorAction 'Stop' # Verify the Availability Group exists on the supplied primary replica $primaryReplicaAvailabilityGroup = $primaryReplicaServerObject.AvailabilityGroups[$AvailabilityGroupName] diff --git a/source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 b/source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 index e9be11588..8467b46ce 100644 --- a/source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 +++ b/source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 @@ -53,7 +53,7 @@ function Get-TargetResource MessageId = $null } - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { @@ -148,7 +148,7 @@ function Set-TargetResource $MessageId ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { diff --git a/source/DSCResources/DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 b/source/DSCResources/DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 index 5c9de447e..a2bd763d7 100644 --- a/source/DSCResources/DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 +++ b/source/DSCResources/DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 @@ -50,7 +50,7 @@ function Get-TargetResource NotificationMethod = $null } - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { @@ -140,7 +140,7 @@ function Set-TargetResource $NotificationMethod ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { diff --git a/source/DSCResources/DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 b/source/DSCResources/DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 index ce00b80e9..89923d686 100644 --- a/source/DSCResources/DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 +++ b/source/DSCResources/DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 @@ -53,7 +53,7 @@ function Get-TargetResource EmailAddress = $null } - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { @@ -142,7 +142,7 @@ function Set-TargetResource $EmailAddress ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { diff --git a/source/DSCResources/DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 b/source/DSCResources/DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 index 043ae8c39..95b53984e 100644 --- a/source/DSCResources/DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 +++ b/source/DSCResources/DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 @@ -56,7 +56,7 @@ function Get-TargetResource $RestartTimeout = 120 ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $isAlwaysOnEnabled = [System.Boolean] $sqlServerObject.IsHadrEnabled if ($isAlwaysOnEnabled -eq $true) diff --git a/source/DSCResources/DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 b/source/DSCResources/DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 index 8ea888f1f..ae9453984 100644 --- a/source/DSCResources/DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 +++ b/source/DSCResources/DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 @@ -65,7 +65,7 @@ function Get-TargetResource $RestartTimeout = 120 ) - $sql = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sql = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Get the current value of the configuration option. $option = $sql.Configuration.Properties | @@ -148,7 +148,7 @@ function Set-TargetResource $RestartTimeout = 120 ) - $sql = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sql = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Get the current value of the configuration option. $option = $sql.Configuration.Properties | diff --git a/source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 b/source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 index 6bc7fb3bd..dae216fa7 100644 --- a/source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 +++ b/source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 @@ -78,7 +78,7 @@ function Get-TargetResource OwnerName = $null } - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { @@ -187,7 +187,7 @@ function Set-TargetResource $OwnerName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { diff --git a/source/DSCResources/DSC_SqlDatabaseDefaultLocation/DSC_SqlDatabaseDefaultLocation.psm1 b/source/DSCResources/DSC_SqlDatabaseDefaultLocation/DSC_SqlDatabaseDefaultLocation.psm1 index 2b0f86437..732a7f013 100644 --- a/source/DSCResources/DSC_SqlDatabaseDefaultLocation/DSC_SqlDatabaseDefaultLocation.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseDefaultLocation/DSC_SqlDatabaseDefaultLocation.psm1 @@ -56,7 +56,7 @@ function Get-TargetResource Write-Verbose -Message ($script:localizedData.GetCurrentPath -f $Type, $InstanceName) # Connect to the instance - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Is this node actively hosting the SQL instance? $isActiveNode = Test-ActiveNode -ServerObject $sqlServerObject @@ -166,7 +166,7 @@ function Set-TargetResource else { Write-Verbose -Message ($script:localizedData.SettingDefaultPath -f $Type) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Check which default location is being updated switch ($Type) diff --git a/source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 b/source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 index 4bae4b6d0..7fd6e4351 100644 --- a/source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 @@ -83,7 +83,7 @@ function Get-TargetResource -f $ServerName, $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { @@ -293,7 +293,7 @@ function Set-TargetResource -f $ServerName, $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { diff --git a/source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1 b/source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1 index 583f35f3e..d7b1921bc 100644 --- a/source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1 @@ -792,7 +792,7 @@ function Get-DatabaseObject $sqlObject = $null - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { diff --git a/source/DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 b/source/DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 index 3d700b5aa..c71f4dc8c 100644 --- a/source/DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 @@ -80,7 +80,7 @@ function Get-TargetResource $roleStatus = 'Absent' $membersInDesiredState = $false - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { $membersInDesiredState = $true @@ -252,7 +252,7 @@ function Set-TargetResource $script:localizedData.SetDatabaseRoleProperties -f $Name ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { $sqlDatabaseObject = $sqlServerObject.Databases[$DatabaseName] diff --git a/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 b/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 index 2c1ca7a0a..bfc8db6e2 100644 --- a/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 @@ -47,7 +47,7 @@ function Get-TargetResource $DatabaseName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' Write-Verbose -Message ( $script:localizedData.RetrievingDatabaseUser -f $Name, $DatabaseName @@ -756,7 +756,7 @@ function Assert-SqlLogin $LoginName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if (-not $sqlServerObject.Logins[$LoginName]) { @@ -812,7 +812,7 @@ function Assert-DatabaseCertificate $RemainingArguments ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if (-not $sqlServerObject.Databases[$DatabaseName].Certificates[$CertificateName]) { @@ -868,7 +868,7 @@ function Assert-DatabaseAsymmetricKey $RemainingArguments ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if (-not $sqlServerObject.Databases[$DatabaseName].AsymmetricKeys[$AsymmetricKeyName]) { diff --git a/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 b/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 index ddb73ddf8..5d30b3edc 100644 --- a/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 +++ b/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 @@ -75,7 +75,7 @@ function Get-TargetResource MessageForwardingSize = $null } - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { @@ -224,7 +224,7 @@ function Set-TargetResource $getTargetResourceResult = Get-TargetResource @getTargetResourceParameters - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { diff --git a/source/DSCResources/DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 b/source/DSCResources/DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 index bbd79b4f2..e39508872 100644 --- a/source/DSCResources/DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 +++ b/source/DSCResources/DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 @@ -53,7 +53,7 @@ function Get-TargetResource try { - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $endpointObject = $sqlServerObject.Endpoints[$Name] if ( $null -ne $endpointObject ) @@ -169,7 +169,7 @@ function Set-TargetResource $script:localizedData.SetEndpointPermission -f $Name, $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $endpointObject = $sqlServerObject.Endpoints[$Name] if ($null -ne $endpointObject) diff --git a/source/DSCResources/DSC_SqlLogin/DSC_SqlLogin.psm1 b/source/DSCResources/DSC_SqlLogin/DSC_SqlLogin.psm1 index 6380f9437..f6cf48177 100644 --- a/source/DSCResources/DSC_SqlLogin/DSC_SqlLogin.psm1 +++ b/source/DSCResources/DSC_SqlLogin/DSC_SqlLogin.psm1 @@ -44,7 +44,7 @@ function Get-TargetResource $script:localizedData.GetLogin -f $Name, $ServerName, $InstanceName ) - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($serverObject) { $login = $serverObject.Logins[$Name] @@ -182,7 +182,7 @@ function Set-TargetResource $DefaultDatabase ) - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' switch ( $Ensure ) { @@ -567,7 +567,7 @@ function Test-TargetResource try { - Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -SetupCredential $userCredential -LoginType 'SqlLogin' | Out-Null + Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -SetupCredential $userCredential -LoginType 'SqlLogin' -ErrorAction 'Stop' | Out-Null } catch { diff --git a/source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 b/source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 index 7d60ed152..ef1a6ba97 100644 --- a/source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 +++ b/source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 @@ -40,7 +40,7 @@ function Get-TargetResource $currentMaxDop = $null $isActiveNode = $null - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { # Is this node actively hosting the SQL instance? @@ -128,7 +128,7 @@ function Set-TargetResource $ProcessOnlyOnActiveNode ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { Write-Verbose -Message ( diff --git a/source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 b/source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 index a92ddc291..7bbe78443 100644 --- a/source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 +++ b/source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 @@ -38,7 +38,7 @@ function Get-TargetResource $script:localizedData.GetMemoryValues -f $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { $minMemory = $sqlServerObject.Configuration.MinServerMemory.ConfigValue @@ -154,7 +154,7 @@ function Set-TargetResource $script:localizedData.SetNewValues -f $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { switch ($Ensure) diff --git a/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 b/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 index 1e8292ccb..755517af1 100644 --- a/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 +++ b/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 @@ -50,7 +50,7 @@ function Get-TargetResource $ServerRoleName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $ensure = 'Absent' $membersInRole = $null @@ -166,7 +166,7 @@ function Set-TargetResource Assert-BoundParameter @assertBoundParameterParameters - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($sqlServerObject) { diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 index a9f16d336..d2eb7bd9c 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 @@ -2539,7 +2539,7 @@ function Get-SqlEngineProperties #$agentServiceCimInstance = Get-CimInstance -ClassName 'Win32_Service' -Filter ("Name = '{0}'" -f $serviceNames.AgentService) $sqlAgentService = Get-ServiceProperties -ServiceName $serviceNames.AgentService - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $sqlCollation = $sqlServerObject.Collation $isClustered = $sqlServerObject.IsClustered @@ -2742,7 +2742,7 @@ function Get-TempDbProperties $InstanceName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $databaseTempDb = $sqlServerObject.Databases['tempdb'] @@ -2934,7 +2934,7 @@ function Get-SqlRoleMembers $RoleName ) - $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $sqlServerObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' $membersOfSysAdminRole = @($sqlServerObject.Roles[$RoleName].EnumMemberNames()) diff --git a/source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 b/source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 index a5bb15360..0b654ce8a 100644 --- a/source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 +++ b/source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 @@ -76,7 +76,7 @@ function Get-TargetResource ) # Connect to the instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($serverObject) { diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index 0b33012eb..c5af6343f 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -458,7 +458,7 @@ function Start-SqlSetupProcess Connects to the instance 'MyInstance' on the local server. .EXAMPLE - Connect-SQL ServerName 'sql.company.local' -InstanceName 'MyInstance' + Connect-SQL ServerName 'sql.company.local' -InstanceName 'MyInstance' -ErrorAction 'Stop' Connects to the instance 'MyInstance' on the server 'sql.company.local'. #> @@ -569,7 +569,22 @@ function Connect-SQL catch { $errorMessage = $script:localizedData.FailedToConnectToDatabaseEngineInstance -f $databaseEngineInstance - New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ + + $invalidOperationException = New-Object -TypeName 'InvalidOperationException' -ArgumentList @($errorMessage, $_.Exception) + + $newObjectParameters = @{ + TypeName = 'System.Management.Automation.ErrorRecord' + ArgumentList = @( + $invalidOperationException.ToString(), + 'CS0001', + 'InvalidOperation', + $databaseEngineInstance + ) + } + + $errorRecordToThrow = New-Object @newObjectParameters + + Write-Error -ErrorRecord $errorRecordToThrow } finally { @@ -1029,7 +1044,7 @@ function Restart-SqlService if (-not $SkipClusterCheck.IsPresent) { ## Connect to the instance - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' if ($serverObject.IsClustered) { @@ -1470,7 +1485,7 @@ function Invoke-Query $connectSQLParameters.SetupCredential = $DatabaseCredential } - $serverObject = Connect-SQL @connectSQLParameters + $serverObject = Connect-SQL @connectSQLParameters -ErrorAction 'Stop' } $redactedQuery = $Query @@ -1718,7 +1733,7 @@ function Test-AvailabilityReplicaSeedingModeAutomatic # Assume automatic seeding is disabled by default $availabilityReplicaSeedingModeAutomatic = $false - $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName + $serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' # Only check the seeding mode if this is SQL 2016 or newer if ( $serverObject.Version -ge 13 ) @@ -1777,7 +1792,7 @@ function Get-PrimaryReplicaServerObject # Determine if we're connected to the primary replica if ( ( $AvailabilityGroup.PrimaryReplicaServerName -ne $serverObject.DomainInstanceName ) -and ( -not [System.String]::IsNullOrEmpty($AvailabilityGroup.PrimaryReplicaServerName) ) ) { - $primaryReplicaServerObject = Connect-SQL -ServerName $AvailabilityGroup.PrimaryReplicaServerName + $primaryReplicaServerObject = Connect-SQL -ServerName $AvailabilityGroup.PrimaryReplicaServerName -ErrorAction 'Stop' } return $primaryReplicaServerObject diff --git a/tests/Unit/SqlServerDsc.Common.Tests.ps1 b/tests/Unit/SqlServerDsc.Common.Tests.ps1 index 69a4e6a35..a64191b5b 100644 --- a/tests/Unit/SqlServerDsc.Common.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.Common.Tests.ps1 @@ -2922,17 +2922,20 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { BeforeEach { Mock -CommandName Import-SQLPSModule - Mock -CommandName New-Object ` - -MockWith $mockNewObject_MicrosoftDatabaseEngine ` - -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter } Context 'When connecting to the default instance using integrated Windows Authentication' { + BeforeAll { + Mock -CommandName New-Object ` + -MockWith $mockNewObject_MicrosoftDatabaseEngine ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + It 'Should return the correct service instance' { $mockExpectedDatabaseEngineServer = 'TestServer' $mockExpectedDatabaseEngineInstance = 'MSSQLSERVER' - $databaseEngineServerObject = Connect-SQL -ServerName $mockExpectedDatabaseEngineServer + $databaseEngineServerObject = Connect-SQL -ServerName $mockExpectedDatabaseEngineServer -ErrorAction 'Stop' $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly $mockExpectedDatabaseEngineServer Should -Invoke -CommandName New-Object -Exactly -Times 1 -Scope It ` @@ -2941,12 +2944,18 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } Context 'When connecting to the default instance using SQL Server Authentication' { + BeforeAll { + Mock -CommandName New-Object ` + -MockWith $mockNewObject_MicrosoftDatabaseEngine ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + It 'Should return the correct service instance' { $mockExpectedDatabaseEngineServer = 'TestServer' $mockExpectedDatabaseEngineInstance = 'MSSQLSERVER' $mockExpectedDatabaseEngineLoginSecure = $false - $databaseEngineServerObject = Connect-SQL -ServerName $mockExpectedDatabaseEngineServer -SetupCredential $mockSqlCredential -LoginType 'SqlLogin' + $databaseEngineServerObject = Connect-SQL -ServerName $mockExpectedDatabaseEngineServer -SetupCredential $mockSqlCredential -LoginType 'SqlLogin' -ErrorAction 'Stop' $databaseEngineServerObject.ConnectionContext.LoginSecure | Should -Be $false $databaseEngineServerObject.ConnectionContext.Login | Should -Be $mockSqlCredentialUserName $databaseEngineServerObject.ConnectionContext.SecurePassword | Should -Be $mockSqlCredentialSecurePassword @@ -2958,11 +2967,17 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } Context 'When connecting to the named instance using integrated Windows Authentication' { + BeforeAll { + Mock -CommandName New-Object ` + -MockWith $mockNewObject_MicrosoftDatabaseEngine ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + It 'Should return the correct service instance' { $mockExpectedDatabaseEngineServer = $env:COMPUTERNAME - $mockExpectedDatabaseEngineInstance = $mockInstanceName + $mockExpectedDatabaseEngineInstance = 'SqlInstance' - $databaseEngineServerObject = Connect-SQL -InstanceName $mockExpectedDatabaseEngineInstance + $databaseEngineServerObject = Connect-SQL -InstanceName $mockExpectedDatabaseEngineInstance -ErrorAction 'Stop' $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" Should -Invoke -CommandName New-Object -Exactly -Times 1 -Scope It ` @@ -2971,12 +2986,18 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } Context 'When connecting to the named instance using SQL Server Authentication' { + BeforeAll { + Mock -CommandName New-Object ` + -MockWith $mockNewObject_MicrosoftDatabaseEngine ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + It 'Should return the correct service instance' { $mockExpectedDatabaseEngineServer = $env:COMPUTERNAME - $mockExpectedDatabaseEngineInstance = $mockInstanceName + $mockExpectedDatabaseEngineInstance = 'SqlInstance' $mockExpectedDatabaseEngineLoginSecure = $false - $databaseEngineServerObject = Connect-SQL -InstanceName $mockExpectedDatabaseEngineInstance -SetupCredential $mockSqlCredential -LoginType 'SqlLogin' + $databaseEngineServerObject = Connect-SQL -InstanceName $mockExpectedDatabaseEngineInstance -SetupCredential $mockSqlCredential -LoginType 'SqlLogin' -ErrorAction 'Stop' $databaseEngineServerObject.ConnectionContext.LoginSecure | Should -Be $false $databaseEngineServerObject.ConnectionContext.Login | Should -Be $mockSqlCredentialUserName $databaseEngineServerObject.ConnectionContext.SecurePassword | Should -Be $mockSqlCredentialSecurePassword @@ -2988,11 +3009,17 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } Context 'When connecting to the named instance using integrated Windows Authentication and different server name' { + BeforeAll { + Mock -CommandName New-Object ` + -MockWith $mockNewObject_MicrosoftDatabaseEngine ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + } + It 'Should return the correct service instance' { $mockExpectedDatabaseEngineServer = 'SERVER' - $mockExpectedDatabaseEngineInstance = $mockInstanceName + $mockExpectedDatabaseEngineInstance = 'SqlInstance' - $databaseEngineServerObject = Connect-SQL -ServerName $mockExpectedDatabaseEngineServer -InstanceName $mockExpectedDatabaseEngineInstance + $databaseEngineServerObject = Connect-SQL -ServerName $mockExpectedDatabaseEngineServer -InstanceName $mockExpectedDatabaseEngineInstance -ErrorAction 'Stop' $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" Should -Invoke -CommandName New-Object -Exactly -Times 1 -Scope It ` @@ -3002,8 +3029,12 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { Context 'When connecting to the named instance using Windows Authentication impersonation' { BeforeAll { + Mock -CommandName New-Object ` + -MockWith $mockNewObject_MicrosoftDatabaseEngine ` + -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + $mockExpectedDatabaseEngineServer = $env:COMPUTERNAME - $mockExpectedDatabaseEngineInstance = $mockInstanceName + $mockExpectedDatabaseEngineInstance = 'SqlInstance' } Context 'When using the default login type' { @@ -3016,7 +3047,7 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } It 'Should return the correct service instance' { - $databaseEngineServerObject = Connect-SQL @testParameters + $databaseEngineServerObject = Connect-SQL @testParameters -ErrorAction 'Stop' $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true $databaseEngineServerObject.ConnectionContext.ConnectAsUserPassword | Should -BeExactly $mockWinCredential.GetNetworkCredential().Password @@ -3041,7 +3072,7 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } It 'Should return the correct service instance' { - $databaseEngineServerObject = Connect-SQL @testParameters + $databaseEngineServerObject = Connect-SQL @testParameters -ErrorAction 'Stop' $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true $databaseEngineServerObject.ConnectionContext.ConnectAsUserPassword | Should -BeExactly $mockWinCredential.GetNetworkCredential().Password @@ -3065,7 +3096,7 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } It 'Should return the correct service instance' { - $databaseEngineServerObject = Connect-SQL @testParameters + $databaseEngineServerObject = Connect-SQL @testParameters -ErrorAction 'Stop' $databaseEngineServerObject.ConnectionContext.ServerInstance | Should -BeExactly "$mockExpectedDatabaseEngineServer\$mockExpectedDatabaseEngineInstance" $databaseEngineServerObject.ConnectionContext.ConnectAsUser | Should -Be $true $databaseEngineServerObject.ConnectionContext.ConnectAsUserPassword | Should -BeExactly $mockWinFqdnCredential.GetNetworkCredential().Password @@ -3081,26 +3112,90 @@ Describe 'SqlServerDsc.Common\Connect-SQL' -Tag 'ConnectSql' { } Context 'When connecting to the default instance using the correct service instance but does not return a correct Database Engine object' { - It 'Should throw the correct error' { - $mockExpectedDatabaseEngineServer = $env:COMPUTERNAME - $mockExpectedDatabaseEngineInstance = $mockInstanceName + Context 'When using ErrorAction set to Stop' { + BeforeAll { + Mock -CommandName New-Object -ParameterFilter { + $TypeName -eq 'Microsoft.SqlServer.Management.Smo.Server' + } -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType ScriptProperty -Name Status -Value { + return $null + } -PassThru | + Add-Member -MemberType NoteProperty -Name ConnectionContext -Value ( + New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name ServerInstance -Value 'localhost' -PassThru | + Add-Member -MemberType NoteProperty -Name LoginSecure -Value $true -PassThru | + Add-Member -MemberType NoteProperty -Name Login -Value '' -PassThru | + Add-Member -MemberType NoteProperty -Name SecurePassword -Value $null -PassThru | + Add-Member -MemberType NoteProperty -Name ConnectAsUser -Value $false -PassThru | + Add-Member -MemberType NoteProperty -Name ConnectAsUserPassword -Value '' -PassThru | + Add-Member -MemberType NoteProperty -Name ConnectAsUserName -Value '' -PassThru | + Add-Member -MemberType NoteProperty -Name StatementTimeout -Value 600 -PassThru | + Add-Member -MemberType NoteProperty -Name ApplicationName -Value 'SqlServerDsc' -PassThru | + Add-Member -MemberType ScriptMethod -Name Disconnect -Value { + return $true + } -PassThru | + Add-Member -MemberType ScriptMethod -Name Connect -Value { + return + } -PassThru -Force + ) -PassThru -Force + } + } - Mock -CommandName New-Object ` - -MockWith $mockNewObject_MicrosoftDatabaseEngine ` - -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + It 'Should throw the correct error' { + $mockLocalizedString = InModuleScope -ScriptBlock { + $script:localizedData.FailedToConnectToDatabaseEngineInstance + } - $mockLocalizedString = InModuleScope -ScriptBlock { - $script:localizedData.FailedToConnectToDatabaseEngineInstance - } + $mockErrorMessage = $mockLocalizedString -f 'localhost' - $mockErrorRecord = Get-InvalidOperationRecord -Message ( - $mockLocalizedString -f $mockExpectedDatabaseEngineServer - ) + { Connect-SQL -ServerName 'localhost' -ErrorAction 'Stop' } | + Should -Throw -ExpectedMessage ('System.InvalidOperationException: {0}*' -f $mockErrorMessage) - { Connect-SQL } | Should -Throw -ExpectedMessage ($mockErrorRecord.Exception.Message + '*') + Should -Invoke -CommandName New-Object -ParameterFilter { + $TypeName -eq 'Microsoft.SqlServer.Management.Smo.Server' + } -Exactly -Times 1 -Scope It + } + } - Should -Invoke -CommandName New-Object -Exactly -Times 1 -Scope It ` - -ParameterFilter $mockNewObject_MicrosoftDatabaseEngine_ParameterFilter + Context 'When using ErrorAction set to SilentlyContinue' { + BeforeAll { + Mock -CommandName New-Object -ParameterFilter { + $TypeName -eq 'Microsoft.SqlServer.Management.Smo.Server' + } -MockWith { + return New-Object -TypeName Object | + Add-Member -MemberType ScriptProperty -Name Status -Value { + return $null + } -PassThru | + Add-Member -MemberType NoteProperty -Name ConnectionContext -Value ( + New-Object -TypeName Object | + Add-Member -MemberType NoteProperty -Name ServerInstance -Value 'localhost' -PassThru | + Add-Member -MemberType NoteProperty -Name LoginSecure -Value $true -PassThru | + Add-Member -MemberType NoteProperty -Name Login -Value '' -PassThru | + Add-Member -MemberType NoteProperty -Name SecurePassword -Value $null -PassThru | + Add-Member -MemberType NoteProperty -Name ConnectAsUser -Value $false -PassThru | + Add-Member -MemberType NoteProperty -Name ConnectAsUserPassword -Value '' -PassThru | + Add-Member -MemberType NoteProperty -Name ConnectAsUserName -Value '' -PassThru | + Add-Member -MemberType NoteProperty -Name StatementTimeout -Value 600 -PassThru | + Add-Member -MemberType NoteProperty -Name ApplicationName -Value 'SqlServerDsc' -PassThru | + Add-Member -MemberType ScriptMethod -Name Disconnect -Value { + return $true + } -PassThru | + Add-Member -MemberType ScriptMethod -Name Connect -Value { + return + } -PassThru -Force + ) -PassThru -Force + } + } + + It 'Should not throw an exception' { + { Connect-SQL -ServerName 'localhost' -ErrorAction 'SilentlyContinue' } | + Should -Not -Throw + + Should -Invoke -CommandName New-Object -ParameterFilter { + $TypeName -eq 'Microsoft.SqlServer.Management.Smo.Server' + } -Exactly -Times 1 -Scope It + } } } }