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

SqlServerDsc: Replace Import-SQLPSModule #1862

Merged
merged 3 commits into from
Mar 4, 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 @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- SqlServerDsc
- Update `appveyor.yml` to use `dotnet tool install` to install _GitVersion_.
- The private function `Import-SQLPSModule` was replaced throughout with
the public command `Import-SqlDscPreferredModule` ([issue #1848](https://github.com/dsccommunity/SqlServerDsc/issues/1848)).

## [16.1.0] - 2023-02-28

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function Set-TargetResource
$StatementTimeout = 600
)

Import-SQLPSModule
Import-SqlDscPreferredModule

# Connect to the defined instance
$serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -StatementTimeout $StatementTimeout -ErrorAction 'Stop'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function Set-TargetResource
$ProcessOnlyOnActiveNode
)

Import-SQLPSModule
Import-SqlDscPreferredModule

# Connect to the instance
$serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function Set-TargetResource
$serverInstance = "$ServerName\$InstanceName"
}

Import-SQLPSModule
Import-SqlDscPreferredModule

switch ($Ensure)
{
Expand Down
6 changes: 3 additions & 3 deletions source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function Get-TargetResource
$script:localizedData.GetCurrentState -f $protocolNameProperties.DisplayName, $InstanceName, $computerName
)

Import-SQLPSModule
Import-SqlDscPreferredModule

<#
Must connect to the local machine name because $ServerName can point
Expand Down Expand Up @@ -186,7 +186,7 @@ function Get-TargetResource
#>
function Set-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Compare-TargetResourceState')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called when calling Compare-TargetResourceState')]
[CmdletBinding()]
param
(
Expand Down Expand Up @@ -415,7 +415,7 @@ function Set-TargetResource
#>
function Test-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Compare-TargetResourceState')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called when calling Compare-TargetResourceState')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function Get-TargetResource
$script:localizedData.GetCurrentState -f $IpAddressGroup, $InstanceName, $computerName
)

Import-SQLPSModule
Import-SqlDscPreferredModule

<#
Must connect to the local machine name because $ServerName can point
Expand Down Expand Up @@ -229,7 +229,7 @@ function Get-TargetResource
#>
function Set-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Compare-TargetResourceState')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called when calling Compare-TargetResourceState')]
[CmdletBinding()]
param
(
Expand Down Expand Up @@ -493,7 +493,7 @@ function Set-TargetResource
#>
function Test-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Compare-TargetResourceState')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called when calling Compare-TargetResourceState')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
Expand Down
4 changes: 2 additions & 2 deletions source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,11 @@ function Set-TargetResource
$reportingServicesDatabaseRightsScript = Invoke-RsCimMethod @invokeRsCimMethodParameters

<#
Import-SQLPSModule cmdlet will import SQLPS (SQL 2012/14) or SqlServer module (SQL 2016),
Import-SqlDscPreferredModule cmdlet will import SQLPS (SQL 2012/14) or SqlServer module (SQL 2016),
and if importing SQLPS, change directory back to the original one, since SQLPS changes the
current directory to SQLSERVER:\ on import.
#>
Import-SQLPSModule
Import-SqlDscPreferredModule

$invokeSqlCmdParameters = @{
ServerInstance = $reportingServicesConnection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function Get-TargetResource
$script:localizedData.GetCurrentState -f $InstanceName
)

Import-SQLPSModule
Import-SqlDscPreferredModule

$sqlMajorVersion = Get-SqlInstanceMajorVersion -InstanceName $InstanceName
$localSqlName = Get-SqlLocalServerName -InstanceName $InstanceName
Expand Down Expand Up @@ -227,7 +227,7 @@ function Set-TargetResource
$UninstallWithForce = $true
)

Import-SQLPSModule
Import-SqlDscPreferredModule

if (($DistributorMode -eq 'Remote') -and (-not $RemoteDistributor))
{
Expand Down
6 changes: 3 additions & 3 deletions source/DSCResources/DSC_SqlScript/DSC_SqlScript.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
#>
function Get-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called in Invoke-SqlScript')]
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
Expand Down Expand Up @@ -211,7 +211,7 @@ function Get-TargetResource
#>
function Set-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called in Invoke-SqlScript')]
[CmdletBinding()]
param
(
Expand Down Expand Up @@ -341,7 +341,7 @@ function Set-TargetResource
#>
function Test-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called in Invoke-SqlScript')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
#>
function Get-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called in Invoke-SqlScript')]
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
Expand Down Expand Up @@ -211,7 +211,7 @@ function Get-TargetResource
#>
function Set-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called in Invoke-SqlScript')]
[CmdletBinding()]
param
(
Expand Down Expand Up @@ -341,7 +341,7 @@ function Set-TargetResource
#>
function Test-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called in Invoke-SqlScript')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
#>
function Get-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is called when Get-ServiceObject is called')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is called when Get-ServiceObject is called')]
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
Expand Down Expand Up @@ -127,7 +127,7 @@ function Get-TargetResource
#>
function Test-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Get-TargetResource')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is implicitly called when calling Get-TargetResource')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
Expand Down Expand Up @@ -209,7 +209,7 @@ function Test-TargetResource
#>
function Set-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is called when Get-ServiceObject is called')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is called when Get-ServiceObject is called')]
[CmdletBinding()]
param
(
Expand Down Expand Up @@ -326,7 +326,7 @@ function Get-ServiceObject
}

# Load the SMO libraries
Import-SQLPSModule
Import-SqlDscPreferredModule

$verboseMessage = $script:localizedData.ConnectingToWmi -f $ServerName
Write-Verbose -Message $verboseMessage
Expand Down
2 changes: 1 addition & 1 deletion source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ function Set-TargetResource
matches the latest assemblies in GAC, mitigating for example
issue #1151.
#>
Import-SQLPSModule -Force
Import-SqlDscPreferredModule -Force
}

if (-not (Test-TargetResource @PSBoundParameters))
Expand Down
6 changes: 3 additions & 3 deletions source/DSCResources/DSC_SqlTraceFlag/DSC_SqlTraceFlag.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Get-TargetResource
)

# Import SqlServer module.
Import-SQLPSModule
Import-SqlDscPreferredModule

Write-Verbose -Message (
$script:localizedData.GetConfiguration -f $InstanceName
Expand Down Expand Up @@ -188,7 +188,7 @@ function Set-TargetResource
)

# Import SqlServer module.
Import-SQLPSModule
Import-SqlDscPreferredModule

Write-Verbose -Message (
$script:localizedData.SetConfiguration -f $InstanceName
Expand Down Expand Up @@ -363,7 +363,7 @@ function Set-TargetResource
#>
function Test-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is called when Get-TargetResource is called')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SqlDscPreferredModule is called when Get-TargetResource is called')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
'Connect-SQL'
'Connect-SQLAnalysis'
'Get-SqlInstanceMajorVersion'
'Import-SQLPSModule'
'Restart-SqlService'
'Restart-ReportingServicesService'
'Invoke-Query'
Expand Down
29 changes: 3 additions & 26 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ function Connect-SQL
$StatementTimeout = 600
)

Import-SQLPSModule
Import-SqlDscPreferredModule

if ($InstanceName -eq 'MSSQLSERVER')
{
Expand Down Expand Up @@ -697,7 +697,7 @@ function Connect-SQLAnalysis
{
if ((Test-FeatureFlag -FeatureFlag $FeatureFlag -TestFlag 'AnalysisServicesConnection'))
{
Import-SQLPSModule
Import-SqlDscPreferredModule

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

Expand Down Expand Up @@ -860,29 +860,6 @@ function Get-SqlInstanceMajorVersion
return $sqlMajorVersionNumber
}

<#
.SYNOPSIS
Imports the module SQLPS in a standardized way.

.PARAMETER Force
Forces the removal of the previous SQL module, to load the same or newer
version fresh. This is meant to make sure the newest version is used, with
the latest assemblies.

#>
function Import-SQLPSModule
{
[CmdletBinding()]
param
(
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Force
)

Import-SqlDscPreferredModule @PSBoundParameters
}

<#
.SYNOPSIS
Restarts a SQL Server instance and associated services
Expand Down Expand Up @@ -2106,7 +2083,7 @@ function Invoke-SqlScript
$Encrypt
)

Import-SQLPSModule
Import-SqlDscPreferredModule

if ($PSCmdlet.ParameterSetName -eq 'File')
{
Expand Down
10 changes: 5 additions & 5 deletions tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<#
.SYNOPSIS
Validates that each *-TargetResource calls Import-SQLPSModule.
Validates that each *-TargetResource calls Import-SqlDscPreferredModule.

.DESCRIPTION
Every *-TargetResource function should include a call to Import-SQLPSModule
Every *-TargetResource function should include a call to Import-SqlDscPreferredModule
so that SMO assemblies are loaded into the PowerShell session.

.EXAMPLE
Expand Down Expand Up @@ -53,18 +53,18 @@ function Measure-CommandsNeededToLoadSMO
$astFilter = {
$args[0] -is [System.Management.Automation.Language.CommandAst] `
-and (
$args[0].CommandElements.Value -eq 'Import-SQLPSModule' `
$args[0].CommandElements.Value -eq 'Import-SqlDscPreferredModule' `
-or $args[0].CommandElements.Value -eq 'Connect-SQL'
)
}

# Find all command calls of Import-SQLPSModule in the function.
# Find all command calls of Import-SqlDscPreferredModule in the function.
$commandAsts = $FunctionAst.FindAll($astFilter, $true)

# If no calls was found then an error record should be returned.
if (-not $commandAsts)
{
$diagnosticRecord['Message'] = 'The function is not calling Import-SQLPSModule or Connect-SQL. If it is meant not to, then suppress the rule ''SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO'' with a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.'
$diagnosticRecord['Message'] = 'The function is not calling Import-SqlDscPreferredModule or Connect-SQL. If it is meant not to, then suppress the rule ''SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO'' with a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.'
$diagnosticRecord -as $diagnosticRecordType
}
}
Expand Down
Loading