Skip to content

Commit

Permalink
SqlSetup: Connect-SQLAnalysis no longer using LoadWithPartialName() (#…
Browse files Browse the repository at this point in the history
…1692)

- SqlSetup
  - The helper function `Connect-SqlAnalysis` was using `LoadWithPartial()`
    to load the assembly _Microsoft.AnalysisServices_. On a node where multiple
    instances with different versions of SQL Server (regardless of features)
    is installed, this will result in the first assembly found in the
    GAC will be loaded into the session, not taking versions into account.
    This can result in an assembly version being loaded that is not compatible
    with the version of SQL Server it was meant to be used with.
    A new method of loading the assembly _Microsoft.AnalysisServices_ was
    introduced under a feature flag; `'AnalysisServicesConnection'`.
    This new functionality depends on the [SqlServer](https://www.powershellgallery.com/packages/SqlServer)
    module, and must be present on the node. The [SqlServer](https://www.powershellgallery.com/packages/SqlServer)
    module can be installed on the node by leveraging the new DSC resource
    `PSModule` in the [PowerShellGet](https://www.powershellgallery.com/packages/PowerShellGet/2.1.2)
    module (v2.1.2 and higher). This new method does not work with the
    SQLPS module due to the SQLPS module does not load the correct assembly,
    while [SqlServer](https://www.powershellgallery.com/packages/SqlServer)
    module (v21.1.18080 and above) does. The new functionality is used
    when the parameter `FeatureFlag` is set to `'AnalysisServicesConnection'`.
    This functionality will be the default in a future breaking release.
  - Under a feature flag; `'AnalysisServicesConnection'`. The detection of
    a successful connection to the SQL Server Analysis Services has also been
    changed. Now it actually evaluates the property `Connected` of the returned
    `Microsoft.AnalysisServices.Server` object. The new functionality is used
    when the parameter `FeatureFlag` is set to `'AnalysisServicesConnection'`.
    This functionality will be the default in a future breaking release.
  • Loading branch information
johlju authored Feb 22, 2021
1 parent fcf882e commit f138dc1
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 147 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- SqlSetup
- The helper function `Connect-SqlAnalysis` was using `LoadWithPartial()`
to load the assembly _Microsoft.AnalysisServices_. On a node where multiple
instances with different versions of SQL Server (regardless of features)
is installed, this will result in the first assembly found in the
GAC will be loaded into the session, not taking versions into account.
This can result in an assembly version being loaded that is not compatible
with the version of SQL Server it was meant to be used with.
A new method of loading the assembly _Microsoft.AnalysisServices_ was
introduced under a feature flag; `'AnalysisServicesConnection'`.
This new functionality depends on the [SqlServer](https://www.powershellgallery.com/packages/SqlServer)
module, and must be present on the node. The [SqlServer](https://www.powershellgallery.com/packages/SqlServer)
module can be installed on the node by leveraging the new DSC resource
`PSModule` in the [PowerShellGet](https://www.powershellgallery.com/packages/PowerShellGet/2.1.2)
module (v2.1.2 and higher). This new method does not work with the
SQLPS module due to the SQLPS module does not load the correct assembly,
while [SqlServer](https://www.powershellgallery.com/packages/SqlServer)
module (v21.1.18080 and above) does. The new functionality is used
when the parameter `FeatureFlag` is set to `'AnalysisServicesConnection'`.
This functionality will be the default in a future breaking release.
- Under a feature flag `'AnalysisServicesConnection'`. The detection of
a successful connection to the SQL Server Analysis Services has also been
changed. Now it actually evaluates the property `Connected` of the returned
`Microsoft.AnalysisServices.Server` object. The new functionality is used
when the parameter `FeatureFlag` is set to `'AnalysisServicesConnection'`.
This functionality will be the default in a future breaking release.

## [15.1.1] - 2021-02-12

### Fixed
Expand Down
1 change: 1 addition & 0 deletions RequiredModules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
PSDscResources = '2.12.0.0'
StorageDsc = '4.9.0.0'
NetworkingDsc = '7.4.0.0'
PowerShellGet = '2.1.2'
}

32 changes: 1 addition & 31 deletions source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ function Get-TargetResource
$getTargetResourceReturnValue.ASSvcAccountUsername = $serviceAnalysisService.UserName
$getTargetResourceReturnValue.AsSvcStartupType = $serviceAnalysisService.StartupType

$analysisServer = Connect-SQLAnalysis -ServerName $sqlHostName -InstanceName $InstanceName
$analysisServer = Connect-SQLAnalysis -ServerName $sqlHostName -InstanceName $InstanceName -FeatureFlag $FeatureFlag

$getTargetResourceReturnValue.ASCollation = $analysisServer.ServerProperties['CollationName'].Value
$getTargetResourceReturnValue.ASDataDir = $analysisServer.ServerProperties['DataDir'].Value
Expand Down Expand Up @@ -2505,36 +2505,6 @@ function Get-InstalledSharedFeatures
return $sharedFeatures
}

<#
.SYNOPSIS
Test if the specific feature flag should be enabled.
.PARAMETER FeatureFlag
An array of feature flags that should be compared against.
.PARAMETER TestFlag
The feature flag that is being check if it should be enabled.
#>
function Test-FeatureFlag
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[System.String[]]
$FeatureFlag,

[Parameter(Mandatory = $true)]
[System.String]
$TestFlag
)

$flagEnabled = $FeatureFlag -and ($FeatureFlag -and $FeatureFlag.Contains($TestFlag))

return $flagEnabled
}

<#
.SYNOPSIS
Get current properties for the feature SQLENGINE.
Expand Down
5 changes: 3 additions & 2 deletions source/DSCResources/DSC_SqlSetup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ more feature flags can be added to the parameter `FeatureFlag`, i.e.
>from one release to another, including having breaking changes.
<!-- markdownlint-disable MD013 -->
Flag | Description
Feature flag | Description
--- | ---
\- | -
DetectionSharedFeatures | A new way of detecting if the shared features is installed or not. This was implemented because the previous implementation did not work fully with SQL Server 2017.
AnalysisServicesConnection | A new method of loading the assembly *Microsoft.AnalysisServices*. Using this, no longer is the helper function `Connect-SqlAnalysis` using `LoadWithPartial()` to load the assembly **Microsoft.AnalysisServices**. This requires the [SqlServer module](https://www.powershellgallery.com/packages/SqlServer) to be present on the node.
<!-- markdownlint-enable MD013 -->

## Known issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'Set-PSModulePath'
'ConvertTo-ServerInstanceName'
'Get-FilePathMajorVersion'
'Test-FeatureFlag'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
81 changes: 70 additions & 11 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,12 @@ function Connect-SQLAnalysis
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$SetupCredential
)
$SetupCredential,

$null = Import-Assembly -Name 'Microsoft.AnalysisServices' -LoadWithPartialName
[Parameter()]
[System.String[]]
$FeatureFlag
)

if ($InstanceName -eq 'MSSQLSERVER')
{
Expand All @@ -646,20 +648,47 @@ function Connect-SQLAnalysis

try
{
$analysisServicesObject = New-Object -TypeName 'Microsoft.AnalysisServices.Server'

if ($analysisServicesObject)
if ((Test-FeatureFlag -FeatureFlag $FeatureFlag -TestFlag 'AnalysisServicesConnection'))
{
$analysisServicesObject.Connect($analysisServicesDataSource)
Import-SQLPSModule

$analysisServicesObject = New-Object -TypeName 'Microsoft.AnalysisServices.Server'

if ($analysisServicesObject)
{
$analysisServicesObject.Connect($analysisServicesDataSource)
}

if ((-not $analysisServicesObject) -or ($analysisServicesObject -and $analysisServicesObject.Connected -eq $false))
{
$errorMessage = $script:localizedData.FailedToConnectToAnalysisServicesInstance -f $analysisServiceInstance

New-InvalidOperationException -Message $errorMessage
}
else
{
Write-Verbose -Message ($script:localizedData.ConnectedToAnalysisServicesInstance -f $analysisServiceInstance) -Verbose
}
}
else
{
$errorMessage = $script:localizedData.FailedToConnectToAnalysisServicesInstance -f $analysisServiceInstance
$null = Import-Assembly -Name 'Microsoft.AnalysisServices' -LoadWithPartialName

New-InvalidOperationException -Message $errorMessage
}
$analysisServicesObject = New-Object -TypeName 'Microsoft.AnalysisServices.Server'

if ($analysisServicesObject)
{
$analysisServicesObject.Connect($analysisServicesDataSource)
}
else
{
$errorMessage = $script:localizedData.FailedToConnectToAnalysisServicesInstance -f $analysisServiceInstance

Write-Verbose -Message ($script:localizedData.ConnectedToAnalysisServicesInstance -f $analysisServiceInstance) -Verbose
New-InvalidOperationException -Message $errorMessage
}

Write-Verbose -Message ($script:localizedData.ConnectedToAnalysisServicesInstance -f $analysisServiceInstance) -Verbose
}
}
catch
{
Expand Down Expand Up @@ -2400,3 +2429,33 @@ function Get-FilePathMajorVersion

(Get-Item -Path $Path).VersionInfo.ProductVersion.Split('.')[0]
}

<#
.SYNOPSIS
Test if the specific feature flag should be enabled.
.PARAMETER FeatureFlag
An array of feature flags that should be compared against.
.PARAMETER TestFlag
The feature flag that is being check if it should be enabled.
#>
function Test-FeatureFlag
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[System.String[]]
$FeatureFlag,

[Parameter(Mandatory = $true)]
[System.String]
$TestFlag
)

$flagEnabled = $FeatureFlag -and ($FeatureFlag -and $FeatureFlag.Contains($TestFlag))

return $flagEnabled
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ConvertFrom-StringData @'
ConnectedToDatabaseEngineInstance = Connected to SQL instance '{0}'. (SQLCOMMON0018)
FailedToConnectToDatabaseEngineInstance = Failed to connect to SQL instance '{0}'. (SQLCOMMON0019)
ConnectedToAnalysisServicesInstance = Connected to Analysis Services instance '{0}'. (SQLCOMMON0020)
FailedToConnectToAnalysisServicesInstance = Failed to connected to Analysis Services instance '{0}'. (SQLCOMMON0021)
FailedToConnectToAnalysisServicesInstance = Failed to connect to Analysis Services instance '{0}'. (SQLCOMMON0021)
SqlServerVersionIsInvalid = Could not get the SQL version for the instance '{0}'. (SQLCOMMON0022)
PreferredModuleFound = Preferred module SqlServer found. (SQLCOMMON0023)
PreferredModuleNotFound = Information: PowerShell module SqlServer not found, trying to use older SQLPS module. (SQLCOMMON0024)
Expand Down
Loading

0 comments on commit f138dc1

Please sign in to comment.