Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect-SQL: Returns clear error message when status is not Online #1841

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `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)).
- Now returns a more clear error message when the status of a database
instance is not `Online`.

### Fixed

Expand Down
38 changes: 35 additions & 3 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,15 @@ function Connect-SQL
{
$sqlConnectionContext.Connect()

if ($sqlServerObject.Status -match '^Online$')
$instanceStatus = $sqlServerObject.Status

if ($instanceStatus)
{
# Property Status is of type Enum ServerStatus, we return the string equivalent.
$instanceStatus = $instanceStatus.ToString()
}

if ($instanceStatus -match '^Online$')
{
Write-Verbose -Message (
$script:localizedData.ConnectedToDatabaseEngineInstance -f $databaseEngineInstance
Expand All @@ -563,7 +571,31 @@ function Connect-SQL
}
else
{
throw
if ([System.String]::IsNullOrEmpty($instanceStatus))
{
$instanceStatus = 'Unknown'
}

$errorMessage = $script:localizedData.DatabaseEngineInstanceNotOnline -f @(
$databaseEngineInstance,
$instanceStatus
)

$invalidOperationException = New-Object -TypeName 'InvalidOperationException' -ArgumentList @($errorMessage)

$newObjectParameters = @{
TypeName = 'System.Management.Automation.ErrorRecord'
ArgumentList = @(
$invalidOperationException.ToString(),
'CS0001',
'InvalidOperation',
$databaseEngineInstance
)
}

$errorRecordToThrow = New-Object @newObjectParameters

Write-Error -ErrorRecord $errorRecordToThrow
}
}
catch
Expand All @@ -576,7 +608,7 @@ function Connect-SQL
TypeName = 'System.Management.Automation.ErrorRecord'
ArgumentList = @(
$invalidOperationException.ToString(),
'CS0001',
'CS0002',
'InvalidOperation',
$databaseEngineInstance
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ ConvertFrom-StringData @'
LoadedAssembly = Loaded the assembly '{0}'. (SQLCOMMON0068)
FailedToLoadAssembly = Failed to load the assembly '{0}'. (SQLCOMMON0069)
FailedToObtainServerInstance = Failed to obtain a SQL Server instance with name '{0}' on server '{1}'. Ensure the SQL Server instance exists on the server and that the 'SQLServer' module references a version of the 'Microsoft.SqlServer.Management.Smo.Wmi' library that supports the version of the SQL Server instance. (SQLCOMMON0070)
DatabaseEngineInstanceNotOnline = The SQL instance '{0}' was expected to have the status 'Online', but had status '{1}'. (SQLCOMMON0071)
'@
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ ConvertFrom-StringData @'
LoadedAssembly = Loaded the assembly '{0}'. (SQLCOMMON0068)
FailedToLoadAssembly = Failed to load the assembly '{0}'. (SQLCOMMON0069)
FailedToObtainServerInstance = Failed to obtain a SQL Server instance with name '{0}' on server '{1}'. Ensure the SQL Server instance exists on the server and that the 'SQLServer' module references a version of the 'Microsoft.SqlServer.Management.Smo.Wmi' library that supports the version of the SQL Server instance. (SQLCOMMON0070)
DatabaseEngineInstanceNotOnline = The SQL instance '{0}' was expected to have the status 'Online', but had status '{1}'. (SQLCOMMON0071)
'@