Skip to content

Commit

Permalink
SqlServerDsc: Connect-Sql now handles ErrorAction correctly (#1839)
Browse files Browse the repository at this point in the history
- SqlServerDsc.Common
  - `Connect-SQL`
    - Was updated to handle both `-ErrorAction 'Stop'` and `-ErrorAction 'SilentlyContinue'`
      when passed to the command ([issue #1837]).
  • Loading branch information
johlju authored Jan 24, 2023
1 parent 3286974 commit 979800d
Show file tree
Hide file tree
Showing 27 changed files with 207 additions and 93 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions source/Classes/011.SqlResourceBase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class SqlResourceBase : ResourceBase
$connectSqlDscDatabaseEngineParameters = @{
ServerName = $this.ServerName
InstanceName = $this.InstanceName
ErrorAction = 'Stop'
}

if ($this.Credential)
Expand Down
4 changes: 2 additions & 2 deletions source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down
18 changes: 9 additions & 9 deletions source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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 )
Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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) )
Expand Down
8 changes: 4 additions & 4 deletions source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
{
Expand Down Expand Up @@ -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])
{
Expand Down Expand Up @@ -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])
{
Expand Down
4 changes: 2 additions & 2 deletions source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
Loading

0 comments on commit 979800d

Please sign in to comment.