From a22490d196093474c483df56d47faf92ffc4279e Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Thu, 28 Jan 2021 18:01:38 +0100 Subject: [PATCH 01/25] Add a new script analyzer rule - A new script analyzer rule to verify that `Import-SQLPSModule` is present in each *-TargetResource. If it is not needed then the analyzer rule should be overridden. --- .vscode/analyzersettings.psd1 | 6 +- CHANGELOG.md | 17 +++--- .../Measure-ImportSQLPSModuleCommand.ps1 | 57 +++++++++++++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 tests/QA/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 diff --git a/.vscode/analyzersettings.psd1 b/.vscode/analyzersettings.psd1 index 4b677606c..72d0b6094 100644 --- a/.vscode/analyzersettings.psd1 +++ b/.vscode/analyzersettings.psd1 @@ -1,5 +1,9 @@ @{ - CustomRulePath = '.\output\RequiredModules\DscResource.AnalyzerRules' + # There is a bug when using an array of paths here. No custom rules are used at all. + CustomRulePath = @( + '.\output\RequiredModules\DscResource.AnalyzerRules' + '.\tests\QA\AnalyzerRules' + ) IncludeDefaultRules = $true IncludeRules = @( # DSC Resource Kit style guideline rules. diff --git a/CHANGELOG.md b/CHANGELOG.md index 8131255ec..f265ecfd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- SqlTraceFlag - - Fixed Assembly not loaded error - ([issue #1680](https://github.com/dsccommunity/SqlServerDsc/issues/1680)). +### Added + +- SqlServerDsc + - A new script analyzer rule to verify that `Import-SQLPSModule` is + present in each *-TargetResource. If it is not needed then the analyzer + rule should be overridden. + - Added unit tests and integration tests for SQL Server 2019 + ([issue #1310](https://github.com/dsccommunity/SqlServerDsc/issues/1310)). ### Changed @@ -29,10 +34,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 where `PasswordExpirationEnabled` is `$true` but `PasswordPolicyEnforced` is `$false`). -- SqlServerDsc - - Added unit tests and integration tests for SQL Server 2019 - ([issue #1310](https://github.com/dsccommunity/SqlServerDsc/issues/1310)). - ### Fixed - SqlServerDsc @@ -56,6 +57,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 argument is no longer wrongly added ([issue #1401](https://github.com/dsccommunity/SqlServerDsc/issues/1401)). - SqlSetup - Added/corrected `InstallSharedDir`, property output when using SQL Server 2019. +- SqlTraceFlag + - Fixed Assembly not loaded error ([issue #1680](https://github.com/dsccommunity/SqlServerDsc/issues/1680)). ## [15.0.1] - 2021-01-09 diff --git a/tests/QA/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 b/tests/QA/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 new file mode 100644 index 000000000..34befefc1 --- /dev/null +++ b/tests/QA/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 @@ -0,0 +1,57 @@ +<# + .SYNOPSIS + Validates that each *-TargetResource calls Import-SQLPSModule. + + .DESCRIPTION + Every *-TargetResource function should include a call to Import-SQLPSModule + so that SMO assemblies are loaded into the PowerShell session. + + .EXAMPLE + Measure-ImportSQLPSModuleCommand -WhileStatementAst $ScriptBlockAst + + .INPUTS + [System.Management.Automation.Language.CommandAst] + + .OUTPUTS + [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] + + .NOTES + None +#> +function Measure-ImportSQLPSModuleCommand +{ + [CmdletBinding()] + [OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.Management.Automation.Language.CommandAst] + $ImportSQLPSModuleCommandAst + ) + + try + { + $diagnosticRecordType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] + $diagnosticRecord = @{ + Message = '' + Extent = $null + RuleName = $null + Severity = 'Warning' + } + + Write-Verbose 'eh' -verbose + $diagnosticRecord['Extent'] = $ImportSQLPSModuleCommandAst.Extent + $diagnosticRecord['RuleName'] = $PSCmdlet.MyInvocation.InvocationName + + if ($ImportSQLPSModuleCommandAst.CommandElements.Value -eq 'Get-ComputerName') + { + $script:diagnosticRecord['Message'] = 'The function is not calling Import-SQLPSModule' + $script:diagnosticRecord -as $diagnosticRecordType + } + } + catch + { + $PSCmdlet.ThrowTerminatingError($PSItem) + } +} From 6d749d434278d5bfc43417b3ce19791c8aa4c0eb Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 29 Jan 2021 18:32:06 +0100 Subject: [PATCH 02/25] Update new changes --- .vscode/analyzersettings.psd1 | 2 +- debug.ps1 | 6 + .../Measure-ImportSQLPSModuleCommand.ps1 | 14 +- ...Measure-ImportSQLPSModuleCommand.Tests.ps1 | 168 ++++++++++++++++++ 4 files changed, 181 insertions(+), 9 deletions(-) create mode 100644 debug.ps1 rename {tests/QA => source}/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 (74%) create mode 100644 tests/Unit/Measure-ImportSQLPSModuleCommand.Tests.ps1 diff --git a/.vscode/analyzersettings.psd1 b/.vscode/analyzersettings.psd1 index 72d0b6094..49e4318a0 100644 --- a/.vscode/analyzersettings.psd1 +++ b/.vscode/analyzersettings.psd1 @@ -2,7 +2,7 @@ # There is a bug when using an array of paths here. No custom rules are used at all. CustomRulePath = @( '.\output\RequiredModules\DscResource.AnalyzerRules' - '.\tests\QA\AnalyzerRules' + '.\source\AnalyzerRules' ) IncludeDefaultRules = $true IncludeRules = @( diff --git a/debug.ps1 b/debug.ps1 new file mode 100644 index 000000000..40a9b4d12 --- /dev/null +++ b/debug.ps1 @@ -0,0 +1,6 @@ +cd C:\source\HelpUsers\johlju\SqlServerDsc +Invoke-ScriptAnalyzer ` + -Path .\output\SqlServerDsc\15.0.1\DSCResources\DSC_SqlTraceFlag\*.psm1 ` + -CustomRulePath C:\source\HelpUsers\johlju\SqlServerDsc\source\AnalyzerRules ` + -IncludeRule @('Measure-*', 'PSDSCDscTestsPresent') ` + -IncludeDefaultRules diff --git a/tests/QA/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 b/source/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 similarity index 74% rename from tests/QA/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 rename to source/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 index 34befefc1..d7af43ff2 100644 --- a/tests/QA/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 +++ b/source/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 @@ -16,7 +16,7 @@ [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] .NOTES - None + None. #> function Measure-ImportSQLPSModuleCommand { @@ -25,9 +25,8 @@ function Measure-ImportSQLPSModuleCommand param ( [Parameter(Mandatory = $true)] - [ValidateNotNullOrEmpty()] [System.Management.Automation.Language.CommandAst] - $ImportSQLPSModuleCommandAst + $CommandAst ) try @@ -40,14 +39,13 @@ function Measure-ImportSQLPSModuleCommand Severity = 'Warning' } - Write-Verbose 'eh' -verbose - $diagnosticRecord['Extent'] = $ImportSQLPSModuleCommandAst.Extent + $diagnosticRecord['Extent'] = $CommandAst.Extent $diagnosticRecord['RuleName'] = $PSCmdlet.MyInvocation.InvocationName - if ($ImportSQLPSModuleCommandAst.CommandElements.Value -eq 'Get-ComputerName') + if ($CommandAst.CommandElements.Value -ne 'Import-SQLPSModule') { - $script:diagnosticRecord['Message'] = 'The function is not calling Import-SQLPSModule' - $script:diagnosticRecord -as $diagnosticRecordType + $diagnosticRecord['Message'] = 'The function is not calling Import-SQLPSModule' + $diagnosticRecord -as $diagnosticRecordType } } catch diff --git a/tests/Unit/Measure-ImportSQLPSModuleCommand.Tests.ps1 b/tests/Unit/Measure-ImportSQLPSModuleCommand.Tests.ps1 new file mode 100644 index 000000000..4506fcc9c --- /dev/null +++ b/tests/Unit/Measure-ImportSQLPSModuleCommand.Tests.ps1 @@ -0,0 +1,168 @@ +$customAnalyzerRulesPath = Join-Path -Path $PSScriptRoot -ChildPath '\..\..\source\AnalyzerRules' + +. $customAnalyzerRulesPath\Measure-ImportSQLPSModuleCommand.ps1 + +<# + .SYNOPSIS + Helper function to return Ast objects, + to be able to test custom rules. + + .PARAMETER ScriptDefinition + The script definition to return ast for. + + .PARAMETER AstType + The Ast type to return; + System.Management.Automation.Language.ParameterAst, + System.Management.Automation.Language.NamedAttributeArgumentAst, + etc. +#> +function Get-AstFromDefinition +{ + [CmdletBinding()] + [OutputType([System.Management.Automation.Language.Ast[]])] + param + ( + [Parameter(Mandatory = $true)] + [System.String] + $ScriptDefinition, + + [Parameter(Mandatory = $true)] + [System.String] + $AstType + ) + + $parseErrors = $null + + $definitionAst = [System.Management.Automation.Language.Parser]::ParseInput($ScriptDefinition, [ref] $null, [ref] $parseErrors) + + if ($parseErrors) + { + throw $parseErrors + } + + $astFilter = { + $args[0] -is $AstType + } + + $foundAsts = $definitionAst.FindAll($astFilter, $true) + + return $foundAsts +} + +Describe 'Measure-ImportSQLPSModuleCommand' { + Context 'When calling the function directly' { + BeforeAll { + $astType = 'System.Management.Automation.Language.CommandAst' + $ruleName = 'Measure-ImportSQLPSModuleCommand' + } + + Context 'When Get-TargetResource is missing call to Import-SQLPSModule' { + It 'Should write the correct error record' { + $definition = ' + function Get-Something {} + + function Get-TargetResource + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Get-Something + + return @{} + } + ' + + $mockAst = Get-AstFromDefinition -ScriptDefinition $definition -AstType $astType + + $mockAst | Should -Not -BeNullOrEmpty + + $record = Measure-ImportSQLPSModuleCommand -CommandAst $mockAst[0] + + ($record | Measure-Object).Count | Should -Be 1 + $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' + $record.RuleName | Should -Be $ruleName + } + } + + Context 'When Get-TargetResource have a call to Import-SQLPSModule' { + It 'Should not return an error record' { + $definition = ' + function Import-SQLPSModule {} + + function Get-TargetResource + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Import-SQLPSModule + + return @{} + } + ' + + $mockAst = Get-AstFromDefinition -ScriptDefinition $definition -AstType $astType + + $mockAst | Should -Not -BeNullOrEmpty + + $record = Measure-ImportSQLPSModuleCommand -CommandAst $mockAst[0] + + $record | Should -BeNullOrEmpty + } + } + } + + Context 'When calling PSScriptAnalyzer' { + BeforeAll { + $invokeScriptAnalyzerParameters = @{ + CustomRulePath = $customAnalyzerRulesPath + } + + $ruleName = "ModuleName\Measure-ImportSQLPSModuleCommand" + } + + Context 'When Get-TargetResource is missing call to Import-SQLPSModule' { + It 'Should write the correct error record' { + $invokeScriptAnalyzerParameters['ScriptDefinition'] = ' + function Get-Something {} + + function Get-TargetResource + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Get-Something + + return @{} + } + ' + + $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters + ($record | Measure-Object).Count | Should -BeExactly 1 + $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' + $record.RuleName | Should -Be $ruleName + } + } + + # Context 'When While-statement follows style guideline' { + # It 'Should not write an error record' { + # $invokeScriptAnalyzerParameters['ScriptDefinition'] = ' + # function Get-Something + # { + # $i = 10 + + # while ($i -gt 0) + # { + # $i-- + # } + # } + # ' + + # $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters + # $record | Should -BeNullOrEmpty + # } + # } + } +} From 0f0867aab772945b212877306dd293e0abff57cb Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 08:49:20 +0100 Subject: [PATCH 03/25] Working custom rule in VS Code --- .vscode/analyzersettings.psd1 | 2 +- debug.ps1 | 25 +- .../Measure-ImportSQLPSModuleCommand.ps1 | 55 ---- .../SqlServerDsc.AnalyzerRules.psm1 | 75 ++++++ ...Measure-ImportSQLPSModuleCommand.Tests.ps1 | 168 ------------ .../Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 | 243 ++++++++++++++++++ 6 files changed, 340 insertions(+), 228 deletions(-) delete mode 100644 source/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 create mode 100644 tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 delete mode 100644 tests/Unit/Measure-ImportSQLPSModuleCommand.Tests.ps1 create mode 100644 tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 diff --git a/.vscode/analyzersettings.psd1 b/.vscode/analyzersettings.psd1 index 49e4318a0..b63c68fee 100644 --- a/.vscode/analyzersettings.psd1 +++ b/.vscode/analyzersettings.psd1 @@ -2,7 +2,7 @@ # There is a bug when using an array of paths here. No custom rules are used at all. CustomRulePath = @( '.\output\RequiredModules\DscResource.AnalyzerRules' - '.\source\AnalyzerRules' + '.\tests\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1' ) IncludeDefaultRules = $true IncludeRules = @( diff --git a/debug.ps1 b/debug.ps1 index 40a9b4d12..ce6729973 100644 --- a/debug.ps1 +++ b/debug.ps1 @@ -1,6 +1,23 @@ cd C:\source\HelpUsers\johlju\SqlServerDsc +# Invoke-ScriptAnalyzer ` +# -Path .\output\SqlServerDsc\15.0.1\DSCResources\DSC_SqlTraceFlag\*.psm1 ` +# -CustomRulePath C:\source\HelpUsers\johlju\SqlServerDsc\tests\QA\AnalyzerRules\Measure-ImportSQLPSModuleCommand.psm1 ` +# -IncludeRule @('Measure-*') + Invoke-ScriptAnalyzer ` - -Path .\output\SqlServerDsc\15.0.1\DSCResources\DSC_SqlTraceFlag\*.psm1 ` - -CustomRulePath C:\source\HelpUsers\johlju\SqlServerDsc\source\AnalyzerRules ` - -IncludeRule @('Measure-*', 'PSDSCDscTestsPresent') ` - -IncludeDefaultRules + -CustomRulePath C:\source\HelpUsers\johlju\SqlServerDsc\tests\QA\AnalyzerRules\Measure-ImportSQLPSModuleCommand.psm1 ` + -IncludeRule @('Measure-*') ` + -ScriptDefinition @' +function Get-Something {} + +function Get-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Get-Something + + return @{} +} +'@ diff --git a/source/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 b/source/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 deleted file mode 100644 index d7af43ff2..000000000 --- a/source/AnalyzerRules/Measure-ImportSQLPSModuleCommand.ps1 +++ /dev/null @@ -1,55 +0,0 @@ -<# - .SYNOPSIS - Validates that each *-TargetResource calls Import-SQLPSModule. - - .DESCRIPTION - Every *-TargetResource function should include a call to Import-SQLPSModule - so that SMO assemblies are loaded into the PowerShell session. - - .EXAMPLE - Measure-ImportSQLPSModuleCommand -WhileStatementAst $ScriptBlockAst - - .INPUTS - [System.Management.Automation.Language.CommandAst] - - .OUTPUTS - [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] - - .NOTES - None. -#> -function Measure-ImportSQLPSModuleCommand -{ - [CmdletBinding()] - [OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] - param - ( - [Parameter(Mandatory = $true)] - [System.Management.Automation.Language.CommandAst] - $CommandAst - ) - - try - { - $diagnosticRecordType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] - $diagnosticRecord = @{ - Message = '' - Extent = $null - RuleName = $null - Severity = 'Warning' - } - - $diagnosticRecord['Extent'] = $CommandAst.Extent - $diagnosticRecord['RuleName'] = $PSCmdlet.MyInvocation.InvocationName - - if ($CommandAst.CommandElements.Value -ne 'Import-SQLPSModule') - { - $diagnosticRecord['Message'] = 'The function is not calling Import-SQLPSModule' - $diagnosticRecord -as $diagnosticRecordType - } - } - catch - { - $PSCmdlet.ThrowTerminatingError($PSItem) - } -} diff --git a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 new file mode 100644 index 000000000..7963d2495 --- /dev/null +++ b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 @@ -0,0 +1,75 @@ +<# + .SYNOPSIS + Validates that each *-TargetResource calls Import-SQLPSModule. + + .DESCRIPTION + Every *-TargetResource function should include a call to Import-SQLPSModule + so that SMO assemblies are loaded into the PowerShell session. + + .EXAMPLE + Measure-ImportSQLPSModuleCommand -WhileStatementAst $ScriptBlockAst + + .INPUTS + [System.Management.Automation.Language.CommandAst] + + .OUTPUTS + [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] + + .NOTES + None. +#> +function Measure-ImportSQLPSModuleCommand +{ + [CmdletBinding()] + [OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] + param + ( + [Parameter(Mandatory = $true)] + [System.Management.Automation.Language.FunctionDefinitionAst] + $FunctionAst + ) + + $applyRuleToFunction = @( + 'Get-TargetResource', + 'Test-TargetResource', + 'Set-TargetResource' + ) + + try + { + if ($FunctionAst.Name -in $applyRuleToFunction) + { + $diagnosticRecordType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord] + $diagnosticRecord = @{ + Message = '' + Extent = $null + RuleName = $null + Severity = 'Warning' + } + + $diagnosticRecord['Extent'] = $FunctionAst.Extent + $diagnosticRecord['RuleName'] = $PSCmdlet.MyInvocation.InvocationName + + $astFilter = { + $args[0] -is [System.Management.Automation.Language.CommandAst] ` + -and $args[0].CommandElements.Value -eq 'Import-SQLPSModule' + } + + # Find all command calls of Import-SQLPSModule 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' + $diagnosticRecord -as $diagnosticRecordType + } + } + } + catch + { + $PSCmdlet.ThrowTerminatingError($PSItem) + } +} + +Export-ModuleMember -Function 'Measure-*' diff --git a/tests/Unit/Measure-ImportSQLPSModuleCommand.Tests.ps1 b/tests/Unit/Measure-ImportSQLPSModuleCommand.Tests.ps1 deleted file mode 100644 index 4506fcc9c..000000000 --- a/tests/Unit/Measure-ImportSQLPSModuleCommand.Tests.ps1 +++ /dev/null @@ -1,168 +0,0 @@ -$customAnalyzerRulesPath = Join-Path -Path $PSScriptRoot -ChildPath '\..\..\source\AnalyzerRules' - -. $customAnalyzerRulesPath\Measure-ImportSQLPSModuleCommand.ps1 - -<# - .SYNOPSIS - Helper function to return Ast objects, - to be able to test custom rules. - - .PARAMETER ScriptDefinition - The script definition to return ast for. - - .PARAMETER AstType - The Ast type to return; - System.Management.Automation.Language.ParameterAst, - System.Management.Automation.Language.NamedAttributeArgumentAst, - etc. -#> -function Get-AstFromDefinition -{ - [CmdletBinding()] - [OutputType([System.Management.Automation.Language.Ast[]])] - param - ( - [Parameter(Mandatory = $true)] - [System.String] - $ScriptDefinition, - - [Parameter(Mandatory = $true)] - [System.String] - $AstType - ) - - $parseErrors = $null - - $definitionAst = [System.Management.Automation.Language.Parser]::ParseInput($ScriptDefinition, [ref] $null, [ref] $parseErrors) - - if ($parseErrors) - { - throw $parseErrors - } - - $astFilter = { - $args[0] -is $AstType - } - - $foundAsts = $definitionAst.FindAll($astFilter, $true) - - return $foundAsts -} - -Describe 'Measure-ImportSQLPSModuleCommand' { - Context 'When calling the function directly' { - BeforeAll { - $astType = 'System.Management.Automation.Language.CommandAst' - $ruleName = 'Measure-ImportSQLPSModuleCommand' - } - - Context 'When Get-TargetResource is missing call to Import-SQLPSModule' { - It 'Should write the correct error record' { - $definition = ' - function Get-Something {} - - function Get-TargetResource - { - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param () - - Get-Something - - return @{} - } - ' - - $mockAst = Get-AstFromDefinition -ScriptDefinition $definition -AstType $astType - - $mockAst | Should -Not -BeNullOrEmpty - - $record = Measure-ImportSQLPSModuleCommand -CommandAst $mockAst[0] - - ($record | Measure-Object).Count | Should -Be 1 - $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' - $record.RuleName | Should -Be $ruleName - } - } - - Context 'When Get-TargetResource have a call to Import-SQLPSModule' { - It 'Should not return an error record' { - $definition = ' - function Import-SQLPSModule {} - - function Get-TargetResource - { - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param () - - Import-SQLPSModule - - return @{} - } - ' - - $mockAst = Get-AstFromDefinition -ScriptDefinition $definition -AstType $astType - - $mockAst | Should -Not -BeNullOrEmpty - - $record = Measure-ImportSQLPSModuleCommand -CommandAst $mockAst[0] - - $record | Should -BeNullOrEmpty - } - } - } - - Context 'When calling PSScriptAnalyzer' { - BeforeAll { - $invokeScriptAnalyzerParameters = @{ - CustomRulePath = $customAnalyzerRulesPath - } - - $ruleName = "ModuleName\Measure-ImportSQLPSModuleCommand" - } - - Context 'When Get-TargetResource is missing call to Import-SQLPSModule' { - It 'Should write the correct error record' { - $invokeScriptAnalyzerParameters['ScriptDefinition'] = ' - function Get-Something {} - - function Get-TargetResource - { - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param () - - Get-Something - - return @{} - } - ' - - $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters - ($record | Measure-Object).Count | Should -BeExactly 1 - $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' - $record.RuleName | Should -Be $ruleName - } - } - - # Context 'When While-statement follows style guideline' { - # It 'Should not write an error record' { - # $invokeScriptAnalyzerParameters['ScriptDefinition'] = ' - # function Get-Something - # { - # $i = 10 - - # while ($i -gt 0) - # { - # $i-- - # } - # } - # ' - - # $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters - # $record | Should -BeNullOrEmpty - # } - # } - } -} diff --git a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 new file mode 100644 index 000000000..9244b2609 --- /dev/null +++ b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 @@ -0,0 +1,243 @@ +$customAnalyzerRulesModulePath = Join-Path -Path $PSScriptRoot -ChildPath '\..\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1' + +Import-Module -Name $customAnalyzerRulesModulePath -Force + +<# + .SYNOPSIS + Helper function to return Ast objects, + to be able to test custom rules. + + .PARAMETER ScriptDefinition + The script definition to return ast for. + + .PARAMETER AstType + The Ast type to return; + System.Management.Automation.Language.ParameterAst, + System.Management.Automation.Language.NamedAttributeArgumentAst, + etc. + + .NOTES + This is a helper function for the tests. +#> +function Get-AstFromDefinition +{ + [CmdletBinding()] + [OutputType([System.Management.Automation.Language.Ast[]])] + param + ( + [Parameter(Mandatory = $true)] + [System.String] + $ScriptDefinition, + + [Parameter(Mandatory = $true)] + [System.String] + $AstType + ) + + $parseErrors = $null + + $definitionAst = [System.Management.Automation.Language.Parser]::ParseInput($ScriptDefinition, [ref] $null, [ref] $parseErrors) + + if ($parseErrors) + { + throw $parseErrors + } + + $astFilter = { + $args[0] -is $AstType + } + + $foundAsts = $definitionAst.FindAll($astFilter, $true) + + return $foundAsts +} + +Describe 'Measure-ImportSQLPSModuleCommand' { + Context 'When calling the function directly' { + BeforeAll { + $astType = 'System.Management.Automation.Language.FunctionDefinitionAst' + $ruleName = 'Measure-ImportSQLPSModuleCommand' + + $testCases = @( + @{ + FunctionName = 'Get-TargetResource' + } + @{ + FunctionName = 'Test-TargetResource' + } + @{ + FunctionName = 'Set-TargetResource' + } + ) + } + + Context 'When function is missing call to Import-SQLPSModule' { + It 'Should write the correct error record for function ' -TestCases $testCases { + param + ( + [Parameter()] + [System.String] + $FunctionName + ) + + $definition = " + function Get-Something {} + + function $FunctionName + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Get-Something + + return @{} + } + " + + $mockAst = Get-AstFromDefinition -ScriptDefinition $definition -AstType $astType + + $mockAst | Should -Not -BeNullOrEmpty + + # We should evaluate the second function in the script definition. + $record = Measure-ImportSQLPSModuleCommand -FunctionAst $mockAst[1] + + ($record | Measure-Object).Count | Should -Be 1 + $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' + $record.RuleName | Should -Be $ruleName + } + } + + Context 'When function have a call to Import-SQLPSModule' { + It 'Should not return an error record for function ' -TestCases $testCases { + param + ( + [Parameter()] + [System.String] + $FunctionName + ) + + $definition = " + function Import-SQLPSModule {} + + function $FunctionName + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Import-SQLPSModule + + return @{} + } + " + + $mockAst = Get-AstFromDefinition -ScriptDefinition $definition -AstType $astType + + $mockAst | Should -Not -BeNullOrEmpty + + # We should evaluate the second function in the script definition. + $record = Measure-ImportSQLPSModuleCommand -FunctionAst $mockAst[1] + + $record | Should -BeNullOrEmpty + } + } + } + + Context 'When calling PSScriptAnalyzer' { + BeforeAll { + $invokeScriptAnalyzerParameters = @{ + CustomRulePath = $customAnalyzerRulesModulePath + } + + $ruleName = 'SqlServerDsc.AnalyzerRules\Measure-ImportSQLPSModuleCommand' + } + + Context 'When function is missing call to Import-SQLPSModule' { + It 'Should write the correct error record for function ' -TestCases $testCases { + param + ( + [Parameter()] + [System.String] + $FunctionName + ) + + $invokeScriptAnalyzerParameters['ScriptDefinition'] = " + function Get-Something {} + + function $FunctionName + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Get-Something + + return @{} + } + " + + $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters + ($record | Measure-Object).Count | Should -BeExactly 1 + $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' + $record.RuleName | Should -Be $ruleName + } + } + + Context 'When function is not calling any commands' { + It 'Should write the correct error record for function ' -TestCases $testCases { + param + ( + [Parameter()] + [System.String] + $FunctionName + ) + + $invokeScriptAnalyzerParameters['ScriptDefinition'] = " + function $FunctionName + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + return @{} + } + " + + $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters + ($record | Measure-Object).Count | Should -BeExactly 1 + $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' + $record.RuleName | Should -Be $ruleName + } + } + + Context 'When function is missing call to Import-SQLPSModule' { + It 'Should not write an error record for function ' -TestCases $testCases { + param + ( + [Parameter()] + [System.String] + $FunctionName + ) + + $invokeScriptAnalyzerParameters['ScriptDefinition'] = " + function Import-SQLPSModule {} + + function $FunctionName + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Import-SQLPSModule + + return @{} + } + " + + $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters + $record | Should -BeNullOrEmpty + } + } + } +} From 9364dcc65cd7e6dddaf533219f23772c76e2c3b3 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 09:29:58 +0100 Subject: [PATCH 04/25] Add check for Connect-SQL that implicitly calls Import-SqlPSModule --- CHANGELOG.md | 7 ++++--- debug.ps1 | 13 ++++++++----- .../AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 | 7 +++++-- tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 | 10 +++++++--- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f265ecfd6..9e987c847 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - SqlServerDsc - - A new script analyzer rule to verify that `Import-SQLPSModule` is - present in each *-TargetResource. If it is not needed then the analyzer - rule should be overridden. + - A new script analyzer rule to verify that `Import-SQLPSModule` or `Connect-SQL` + (that implicitly calls `Import-SQLPSModule`) is present in each `Get-`, `Test-`, + and `Set-TargetResource` function. If neither command is not needed then the + analyzer rule should be overridden. - Added unit tests and integration tests for SQL Server 2019 ([issue #1310](https://github.com/dsccommunity/SqlServerDsc/issues/1310)). diff --git a/debug.ps1 b/debug.ps1 index ce6729973..24004d248 100644 --- a/debug.ps1 +++ b/debug.ps1 @@ -1,11 +1,14 @@ cd C:\source\HelpUsers\johlju\SqlServerDsc -# Invoke-ScriptAnalyzer ` -# -Path .\output\SqlServerDsc\15.0.1\DSCResources\DSC_SqlTraceFlag\*.psm1 ` -# -CustomRulePath C:\source\HelpUsers\johlju\SqlServerDsc\tests\QA\AnalyzerRules\Measure-ImportSQLPSModuleCommand.psm1 ` -# -IncludeRule @('Measure-*') +Invoke-ScriptAnalyzer ` + -Path .\output\SqlServerDsc\15.0.1\DSCResources\DSC_SqlTraceFlag\*.psm1 ` + -CustomRulePath .\tests\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1 ` + -IncludeRule @('Measure-*') Invoke-ScriptAnalyzer ` - -CustomRulePath C:\source\HelpUsers\johlju\SqlServerDsc\tests\QA\AnalyzerRules\Measure-ImportSQLPSModuleCommand.psm1 ` + -CustomRulePath @( + '.\output\RequiredModules\DscResource.AnalyzerRules' + '.\tests\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1' + ) ` -IncludeRule @('Measure-*') ` -ScriptDefinition @' function Get-Something {} diff --git a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 index 7963d2495..b360e115e 100644 --- a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 +++ b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 @@ -52,7 +52,10 @@ function Measure-ImportSQLPSModuleCommand $astFilter = { $args[0] -is [System.Management.Automation.Language.CommandAst] ` - -and $args[0].CommandElements.Value -eq 'Import-SQLPSModule' + -and ( + $args[0].CommandElements.Value -eq 'Import-SQLPSModule' ` + -or $args[0].CommandElements.Value -eq 'Connect-SQL' + ) } # Find all command calls of Import-SQLPSModule in the function. @@ -61,7 +64,7 @@ function Measure-ImportSQLPSModuleCommand # If no calls was found then an error record should be returned. if (-not $commandAsts) { - $diagnosticRecord['Message'] = 'The function is not calling Import-SQLPSModule' + $diagnosticRecord['Message'] = 'The function is not calling Import-SQLPSModule or Connect-SQL.' $diagnosticRecord -as $diagnosticRecordType } } diff --git a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 index 9244b2609..cba90f9ce 100644 --- a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 @@ -53,6 +53,10 @@ function Get-AstFromDefinition } Describe 'Measure-ImportSQLPSModuleCommand' { + BeforeAll { + $expectedErrorRecordMessage = 'The function is not calling Import-SQLPSModule or Connect-SQL.' + } + Context 'When calling the function directly' { BeforeAll { $astType = 'System.Management.Automation.Language.FunctionDefinitionAst' @@ -103,7 +107,7 @@ Describe 'Measure-ImportSQLPSModuleCommand' { $record = Measure-ImportSQLPSModuleCommand -FunctionAst $mockAst[1] ($record | Measure-Object).Count | Should -Be 1 - $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' + $record.Message | Should -Be $expectedErrorRecordMessage $record.RuleName | Should -Be $ruleName } } @@ -179,7 +183,7 @@ Describe 'Measure-ImportSQLPSModuleCommand' { $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters ($record | Measure-Object).Count | Should -BeExactly 1 - $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' + $record.Message | Should -Be $expectedErrorRecordMessage $record.RuleName | Should -Be $ruleName } } @@ -206,7 +210,7 @@ Describe 'Measure-ImportSQLPSModuleCommand' { $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters ($record | Measure-Object).Count | Should -BeExactly 1 - $record.Message | Should -Be 'The function is not calling Import-SQLPSModule' + $record.Message | Should -Be $expectedErrorRecordMessage $record.RuleName | Should -Be $ruleName } } From ca9269432242ae179af9bcade38916669a273712 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 12:30:59 +0100 Subject: [PATCH 05/25] Add pipeline job --- azure-pipelines.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index d7417fd11..750d80b60 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -62,6 +62,16 @@ stages: downloadType: 'single' artifactName: 'output' downloadPath: '$(Build.SourcesDirectory)' + - powershell: | + # Workaround for issue https://github.com/dsccommunity/DscResource.Test/issues/100 + Invoke-Pester -Script .\tests\QA #-EnableExit + name: qatest + displayName: 'Run SqlServerDsc QA Test' + - powershell: | + # Workaround for issue https://github.com/dsccommunity/DscResource.Test/issues/100 + Invoke-ScriptAnalyzer -Path .\source -Recurse -Settings .\.vscode\analyzersettings.psd1 -EnableExit + name: testPSSA + displayName: 'Run Script Analyzer Test (SqlServerDsc)' - task: PowerShell@2 name: test displayName: 'Run HQRM Test' From 77401d84e4d652f31fa28a8bd513f5f115fa7a7d Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 12:31:05 +0100 Subject: [PATCH 06/25] Add QA test --- tests/QA/ScriptAnalyzer.Tests.ps1 | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/QA/ScriptAnalyzer.Tests.ps1 diff --git a/tests/QA/ScriptAnalyzer.Tests.ps1 b/tests/QA/ScriptAnalyzer.Tests.ps1 new file mode 100644 index 000000000..6895ebebd --- /dev/null +++ b/tests/QA/ScriptAnalyzer.Tests.ps1 @@ -0,0 +1,66 @@ +<# + .SYNOPSIS + Quality test that runs the Script Analyzer with the Script Analyzer settings + file in the .vscode folder. + + .NOTES + In addition to the custom rules that are part of this repository's Script + Analyzer settings file, it will also run the HQRM test that has been run by + the buold task 'DscResource_Tests_Stop_On_Fail'. When the issue in the + repository DscResource.Test is resolved this should not be needed. See issue + https://github.com/dsccommunity/DscResource.Test/issues/100. +#> + +Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + +if (-not (Test-BuildCategory -Type 'Unit')) +{ + return +} + +Describe 'Script Analyzer Rules' { + BeforeAll { + $repositoryPath = Resolve-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../..') + $sourcePath = Join-Path -Path $repositoryPath -ChildPath 'source' + $scriptAnalyzerSettingsPath = Join-Path -Path $repositoryPath -ChildPath '.vscode\analyzersettings.psd1' + } + + Context 'When there are module files' { + BeforeAll { + $moduleFiles = Get-ChildItem -Path $sourcePath -Recurse -Include '*.psm1' + + $testCases = @() + + foreach ($moduleFile in $moduleFiles) + { + $moduleFilePathNormalized = $moduleFile.FullName -replace '\\', '/' + $repositoryPathNormalized = $repositoryPath -replace '\\', '/' + $escapedRepositoryPath = [System.Text.RegularExpressions.RegEx]::Escape($repositoryPathNormalized) + $relativePath = $moduleFilePathNormalized -replace ($escapedRepositoryPath + '/') + + $testCases += @{ + ScriptPath = $moduleFile.FullName + RelativePath = $relativePath + } + } + } + + It 'Should pass all PS Script Analyzer rules for file ''''' -TestCases $testCases { + param + ( + [Parameter()] + [System.String] + $ScriptPath, + + [Parameter()] + [System.String] + $RelativePath + ) + + $pssaError = Invoke-ScriptAnalyzer -Path $ScriptPath -Settings $scriptAnalyzerSettingsPath + + $report = $pssaError | Format-Table -AutoSize | Out-String -Width 200 + $pssaError | Should -HaveCount 0 -Because "all script analyzer rules should pass.`r`n`r`n $report`r`n" + } + } +} From f6b66e897362e8ae2d08f9b3cb69e5706980f26c Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 12:32:09 +0100 Subject: [PATCH 07/25] Fix QA test --- tests/QA/ScriptAnalyzer.Tests.ps1 | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/QA/ScriptAnalyzer.Tests.ps1 b/tests/QA/ScriptAnalyzer.Tests.ps1 index 6895ebebd..e7f85f2e5 100644 --- a/tests/QA/ScriptAnalyzer.Tests.ps1 +++ b/tests/QA/ScriptAnalyzer.Tests.ps1 @@ -11,13 +11,6 @@ https://github.com/dsccommunity/DscResource.Test/issues/100. #> -Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') - -if (-not (Test-BuildCategory -Type 'Unit')) -{ - return -} - Describe 'Script Analyzer Rules' { BeforeAll { $repositoryPath = Resolve-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../..') From a3be0c08a3271fa618fd6d75f063c0701a360273 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 12:39:39 +0100 Subject: [PATCH 08/25] Update build task --- azure-pipelines.yml | 2 ++ tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 750d80b60..1105bd3a4 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -64,11 +64,13 @@ stages: downloadPath: '$(Build.SourcesDirectory)' - powershell: | # Workaround for issue https://github.com/dsccommunity/DscResource.Test/issues/100 + ./build.ps1 -Task noop Invoke-Pester -Script .\tests\QA #-EnableExit name: qatest displayName: 'Run SqlServerDsc QA Test' - powershell: | # Workaround for issue https://github.com/dsccommunity/DscResource.Test/issues/100 + ./build.ps1 -Task noop Invoke-ScriptAnalyzer -Path .\source -Recurse -Settings .\.vscode\analyzersettings.psd1 -EnableExit name: testPSSA displayName: 'Run Script Analyzer Test (SqlServerDsc)' diff --git a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 index cba90f9ce..8ba900a2c 100644 --- a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 @@ -18,6 +18,13 @@ Import-Module -Name $customAnalyzerRulesModulePath -Force .NOTES This is a helper function for the tests. + + Run the custom rules directly bu running: + + Invoke-ScriptAnalyzer ` + -Path .\source\DSCResources\**\*.psm1 ` + -CustomRulePath .\tests\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1 ` + -IncludeRule @('Measure-*') #> function Get-AstFromDefinition { From 0f5c164b69c1a726bf3e1037ee00b09ddd674c3b Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 12:50:27 +0100 Subject: [PATCH 09/25] Fix pipeline job --- azure-pipelines.yml | 8 +------- tests/QA/ScriptAnalyzer.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 1105bd3a4..4b5522fdd 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -65,15 +65,9 @@ stages: - powershell: | # Workaround for issue https://github.com/dsccommunity/DscResource.Test/issues/100 ./build.ps1 -Task noop - Invoke-Pester -Script .\tests\QA #-EnableExit + Invoke-Pester -Script .\tests\QA -EnableExit name: qatest displayName: 'Run SqlServerDsc QA Test' - - powershell: | - # Workaround for issue https://github.com/dsccommunity/DscResource.Test/issues/100 - ./build.ps1 -Task noop - Invoke-ScriptAnalyzer -Path .\source -Recurse -Settings .\.vscode\analyzersettings.psd1 -EnableExit - name: testPSSA - displayName: 'Run Script Analyzer Test (SqlServerDsc)' - task: PowerShell@2 name: test displayName: 'Run HQRM Test' diff --git a/tests/QA/ScriptAnalyzer.Tests.ps1 b/tests/QA/ScriptAnalyzer.Tests.ps1 index e7f85f2e5..fb30699e3 100644 --- a/tests/QA/ScriptAnalyzer.Tests.ps1 +++ b/tests/QA/ScriptAnalyzer.Tests.ps1 @@ -20,7 +20,7 @@ Describe 'Script Analyzer Rules' { Context 'When there are module files' { BeforeAll { - $moduleFiles = Get-ChildItem -Path $sourcePath -Recurse -Include '*.psm1' + $moduleFiles = Get-ChildItem -Path $sourcePath -Recurse -Include @('*.psm1', '*.ps1') $testCases = @() From eeee9797c93605fc504e2934f088628921ca7b6c Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 12:53:12 +0100 Subject: [PATCH 10/25] Fix pipeline job --- azure-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 4b5522fdd..71693713f 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -62,7 +62,7 @@ stages: downloadType: 'single' artifactName: 'output' downloadPath: '$(Build.SourcesDirectory)' - - powershell: | + - pwsh: | # Workaround for issue https://github.com/dsccommunity/DscResource.Test/issues/100 ./build.ps1 -Task noop Invoke-Pester -Script .\tests\QA -EnableExit From cf5fc430c83da1a61cccbbcceb3e80e9b48f03a6 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 12:53:41 +0100 Subject: [PATCH 11/25] Fix pipeline --- azure-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 71693713f..8ff1fd485 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -66,7 +66,7 @@ stages: # Workaround for issue https://github.com/dsccommunity/DscResource.Test/issues/100 ./build.ps1 -Task noop Invoke-Pester -Script .\tests\QA -EnableExit - name: qatest + name: qualitytest displayName: 'Run SqlServerDsc QA Test' - task: PowerShell@2 name: test From e0896e798dba3ad95fb9956b51e94373657fea58 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 13:07:15 +0100 Subject: [PATCH 12/25] Update CAHNGELOG.md --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e987c847..13930b0f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - A new script analyzer rule to verify that `Import-SQLPSModule` or `Connect-SQL` (that implicitly calls `Import-SQLPSModule`) is present in each `Get-`, `Test-`, and `Set-TargetResource` function. If neither command is not needed then the - analyzer rule should be overridden. + analyzer rule should be overridden ([issue #1683](https://github.com/dsccommunity/SqlServerDsc/issues/1683)). + - Add a new pipeline job that runs Script Analyzer on all PowerShell scripts + in the source folder. The rules are defined by the Script Analyzer settings + file `.vscode\analyzersettings.psd1` (which also the Visual Studio Code + PowerShell extension uses). - Added unit tests and integration tests for SQL Server 2019 ([issue #1310](https://github.com/dsccommunity/SqlServerDsc/issues/1310)). From 20b6a3f47c98a04e8bef243aa35e83e96cf46f3b Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 13:07:22 +0100 Subject: [PATCH 13/25] Make color red --- tests/QA/ScriptAnalyzer.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/QA/ScriptAnalyzer.Tests.ps1 b/tests/QA/ScriptAnalyzer.Tests.ps1 index fb30699e3..651611c41 100644 --- a/tests/QA/ScriptAnalyzer.Tests.ps1 +++ b/tests/QA/ScriptAnalyzer.Tests.ps1 @@ -6,7 +6,7 @@ .NOTES In addition to the custom rules that are part of this repository's Script Analyzer settings file, it will also run the HQRM test that has been run by - the buold task 'DscResource_Tests_Stop_On_Fail'. When the issue in the + the build task 'DscResource_Tests_Stop_On_Fail'. When the issue in the repository DscResource.Test is resolved this should not be needed. See issue https://github.com/dsccommunity/DscResource.Test/issues/100. #> @@ -53,7 +53,7 @@ Describe 'Script Analyzer Rules' { $pssaError = Invoke-ScriptAnalyzer -Path $ScriptPath -Settings $scriptAnalyzerSettingsPath $report = $pssaError | Format-Table -AutoSize | Out-String -Width 200 - $pssaError | Should -HaveCount 0 -Because "all script analyzer rules should pass.`r`n`r`n $report`r`n" + $pssaError | Should -HaveCount 0 -Because "all script analyzer rules should pass.`r`n`r`n ##vso[task.LogIssue type=error;]$report`r`n" } } } From 837b0e74e86767bfd0074d85e4de00ec86a2965c Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 13:22:34 +0100 Subject: [PATCH 14/25] Fix analyzer rule --- tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 | 2 +- tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 index b360e115e..edda818ce 100644 --- a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 +++ b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 @@ -64,7 +64,7 @@ function Measure-ImportSQLPSModuleCommand # 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.' + $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-ImportSQLPSModuleCommand'' and add a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.' $diagnosticRecord -as $diagnosticRecordType } } diff --git a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 index 8ba900a2c..e68e3fe95 100644 --- a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 @@ -61,7 +61,7 @@ function Get-AstFromDefinition Describe 'Measure-ImportSQLPSModuleCommand' { BeforeAll { - $expectedErrorRecordMessage = 'The function is not calling Import-SQLPSModule or Connect-SQL.' + $expectedErrorRecordMessage = 'The function is not calling Import-SQLPSModule or Connect-SQL. If it is meant not to, then suppress the rule ''SqlServerDsc.AnalyzerRules\Measure-ImportSQLPSModuleCommand'' and add a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.' } Context 'When calling the function directly' { From ad5095399b3d8ad126acfb02880426e190df5171 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 13:24:45 +0100 Subject: [PATCH 15/25] Fix QA test --- tests/QA/ScriptAnalyzer.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QA/ScriptAnalyzer.Tests.ps1 b/tests/QA/ScriptAnalyzer.Tests.ps1 index 651611c41..379523aba 100644 --- a/tests/QA/ScriptAnalyzer.Tests.ps1 +++ b/tests/QA/ScriptAnalyzer.Tests.ps1 @@ -53,7 +53,7 @@ Describe 'Script Analyzer Rules' { $pssaError = Invoke-ScriptAnalyzer -Path $ScriptPath -Settings $scriptAnalyzerSettingsPath $report = $pssaError | Format-Table -AutoSize | Out-String -Width 200 - $pssaError | Should -HaveCount 0 -Because "all script analyzer rules should pass.`r`n`r`n ##vso[task.LogIssue type=error;]$report`r`n" + $pssaError | Should -HaveCount 0 -Because "all script analyzer rules should pass.`r`n`r`n $report`r`n" } } } From 78a66d51d55efc1c9c495208db3a743ff655f33e Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 13:25:15 +0100 Subject: [PATCH 16/25] Fix lint errors in SqlAG --- source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 b/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 index 5c6c9bb5f..124cec18e 100644 --- a/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 +++ b/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 @@ -558,6 +558,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-ImportSQLPSModuleCommand', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param From 7ed612410a914eef6157d30ae04862bc8a4d16ea Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 14:19:34 +0100 Subject: [PATCH 17/25] Rename rule --- .../SqlServerDsc.AnalyzerRules.psm1 | 6 +- .../Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 | 88 ++++++++++++++++--- 2 files changed, 79 insertions(+), 15 deletions(-) diff --git a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 index edda818ce..e44eaa1d4 100644 --- a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 +++ b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 @@ -7,7 +7,7 @@ so that SMO assemblies are loaded into the PowerShell session. .EXAMPLE - Measure-ImportSQLPSModuleCommand -WhileStatementAst $ScriptBlockAst + Measure-CommandsNeededToLoadSMO -WhileStatementAst $ScriptBlockAst .INPUTS [System.Management.Automation.Language.CommandAst] @@ -18,7 +18,7 @@ .NOTES None. #> -function Measure-ImportSQLPSModuleCommand +function Measure-CommandsNeededToLoadSMO { [CmdletBinding()] [OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] @@ -64,7 +64,7 @@ function Measure-ImportSQLPSModuleCommand # 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-ImportSQLPSModuleCommand'' and add a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.' + $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'' and add a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.' $diagnosticRecord -as $diagnosticRecordType } } diff --git a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 index e68e3fe95..2c945c594 100644 --- a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 @@ -59,15 +59,15 @@ function Get-AstFromDefinition return $foundAsts } -Describe 'Measure-ImportSQLPSModuleCommand' { +Describe 'Measure-CommandsNeededToLoadSMO' { BeforeAll { - $expectedErrorRecordMessage = 'The function is not calling Import-SQLPSModule or Connect-SQL. If it is meant not to, then suppress the rule ''SqlServerDsc.AnalyzerRules\Measure-ImportSQLPSModuleCommand'' and add a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.' + $expectedErrorRecordMessage = 'The function is not calling Import-SQLPSModule or Connect-SQL. If it is meant not to, then suppress the rule ''SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO'' and add a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.' } Context 'When calling the function directly' { BeforeAll { $astType = 'System.Management.Automation.Language.FunctionDefinitionAst' - $ruleName = 'Measure-ImportSQLPSModuleCommand' + $ruleName = 'Measure-CommandsNeededToLoadSMO' $testCases = @( @{ @@ -82,7 +82,7 @@ Describe 'Measure-ImportSQLPSModuleCommand' { ) } - Context 'When function is missing call to Import-SQLPSModule' { + Context 'When a function do not have a call to neither Import-SQLPSModule or Connect-SQL' { It 'Should write the correct error record for function ' -TestCases $testCases { param ( @@ -111,7 +111,7 @@ Describe 'Measure-ImportSQLPSModuleCommand' { $mockAst | Should -Not -BeNullOrEmpty # We should evaluate the second function in the script definition. - $record = Measure-ImportSQLPSModuleCommand -FunctionAst $mockAst[1] + $record = Measure-CommandsNeededToLoadSMO -FunctionAst $mockAst[1] ($record | Measure-Object).Count | Should -Be 1 $record.Message | Should -Be $expectedErrorRecordMessage @@ -119,7 +119,7 @@ Describe 'Measure-ImportSQLPSModuleCommand' { } } - Context 'When function have a call to Import-SQLPSModule' { + Context 'When a function have a call to Import-SQLPSModule' { It 'Should not return an error record for function ' -TestCases $testCases { param ( @@ -148,23 +148,58 @@ Describe 'Measure-ImportSQLPSModuleCommand' { $mockAst | Should -Not -BeNullOrEmpty # We should evaluate the second function in the script definition. - $record = Measure-ImportSQLPSModuleCommand -FunctionAst $mockAst[1] + $record = Measure-CommandsNeededToLoadSMO -FunctionAst $mockAst[1] + + $record | Should -BeNullOrEmpty + } + } + + Context 'When a function have a call to Connect-SQL' { + It 'Should not return an error record for function ' -TestCases $testCases { + param + ( + [Parameter()] + [System.String] + $FunctionName + ) + + $definition = " + function Connect-SQL {} + + function $FunctionName + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Connect-SQL + + return @{} + } + " + + $mockAst = Get-AstFromDefinition -ScriptDefinition $definition -AstType $astType + + $mockAst | Should -Not -BeNullOrEmpty + + # We should evaluate the second function in the script definition. + $record = Measure-CommandsNeededToLoadSMO -FunctionAst $mockAst[1] $record | Should -BeNullOrEmpty } } } - Context 'When calling PSScriptAnalyzer' { + Context 'When using PSScriptAnalyzer' { BeforeAll { $invokeScriptAnalyzerParameters = @{ CustomRulePath = $customAnalyzerRulesModulePath } - $ruleName = 'SqlServerDsc.AnalyzerRules\Measure-ImportSQLPSModuleCommand' + $ruleName = 'SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO' } - Context 'When function is missing call to Import-SQLPSModule' { + Context 'When a function do not have a call to neither Import-SQLPSModule or Connect-SQL' { It 'Should write the correct error record for function ' -TestCases $testCases { param ( @@ -195,7 +230,7 @@ Describe 'Measure-ImportSQLPSModuleCommand' { } } - Context 'When function is not calling any commands' { + Context 'When a function is not calling any commands at all' { It 'Should write the correct error record for function ' -TestCases $testCases { param ( @@ -222,7 +257,7 @@ Describe 'Measure-ImportSQLPSModuleCommand' { } } - Context 'When function is missing call to Import-SQLPSModule' { + Context 'When a function have a call to Import-SQLPSModule' { It 'Should not write an error record for function ' -TestCases $testCases { param ( @@ -250,5 +285,34 @@ Describe 'Measure-ImportSQLPSModuleCommand' { $record | Should -BeNullOrEmpty } } + + Context 'When a function have a call to Connect-SQL' { + It 'Should not write an error record for function ' -TestCases $testCases { + param + ( + [Parameter()] + [System.String] + $FunctionName + ) + + $invokeScriptAnalyzerParameters['ScriptDefinition'] = " + function Connect-SQL {} + + function $FunctionName + { + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param () + + Connect-SQL + + return @{} + } + " + + $record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters + $record | Should -BeNullOrEmpty + } + } } } From cbdf83513781a1986e11dda20200c063a92b7703 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 14:19:42 +0100 Subject: [PATCH 18/25] Fix lint errors --- source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 | 2 +- source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 | 2 ++ source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 | 1 + source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 | 2 +- .../DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 | 1 + .../DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 | 1 + source/DSCResources/DSC_SqlAlias/DSC_SqlAlias.psm1 | 3 +++ .../DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 | 1 + .../DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 | 1 + source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 | 1 + .../DSC_SqlDatabaseDefaultLocation.psm1 | 1 + .../DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 | 1 + .../DSC_SqlDatabaseObjectPermission.psm1 | 3 +++ .../DSC_SqlDatabasePermission/DSC_SqlDatabasePermission.psm1 | 1 + .../DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 | 1 + source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 | 1 + .../DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 | 1 + source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 | 1 + source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 | 1 + source/DSCResources/DSC_SqlPermission/DSC_SqlPermission.psm1 | 1 + source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 | 2 ++ source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 | 3 ++- source/DSCResources/DSC_SqlRSSetup/DSC_SqlRSSetup.psm1 | 3 +++ source/DSCResources/DSC_SqlReplication/DSC_SqlReplication.psm1 | 3 +++ source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 | 1 + source/DSCResources/DSC_SqlScript/DSC_SqlScript.psm1 | 3 +++ source/DSCResources/DSC_SqlScriptQuery/DSC_SqlScriptQuery.psm1 | 3 +++ .../DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 | 3 +++ .../DSC_SqlServiceAccount/DSC_SqlServiceAccount.psm1 | 3 +++ source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 | 2 ++ source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 | 2 ++ .../DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 | 3 +++ .../MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 | 1 + .../MSFT_SqlDatabaseRecoveryModel.psm1 | 1 + .../MSFT_SqlServerEndpointState.psm1 | 1 + .../MSFT_SqlServerNetwork/MSFT_SqlServerNetwork.psm1 | 2 ++ 36 files changed, 60 insertions(+), 3 deletions(-) diff --git a/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 b/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 index 124cec18e..4c84f6685 100644 --- a/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 +++ b/source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1 @@ -558,7 +558,7 @@ function Set-TargetResource #> function Test-TargetResource { - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-ImportSQLPSModuleCommand', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 b/source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 index 6da03afe4..685a9c4f4 100644 --- a/source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 +++ b/source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1 @@ -24,6 +24,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-SQLAlwaysOnAvailabilityGroupListener is called')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -422,6 +423,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 b/source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 index cad99e5a8..cfd19330d 100644 --- a/source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 +++ b/source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1 @@ -550,6 +550,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 b/source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 index b678678c1..2c6eaf3eb 100644 --- a/source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 +++ b/source/DSCResources/DSC_SqlAgentAlert/DSC_SqlAgentAlert.psm1 @@ -202,7 +202,6 @@ function Set-TargetResource New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ } } - } else { @@ -298,6 +297,7 @@ function Set-TargetResource function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 b/source/DSCResources/DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 index 5fa006bc8..7c706e438 100644 --- a/source/DSCResources/DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 +++ b/source/DSCResources/DSC_SqlAgentFailsafe/DSC_SqlAgentFailsafe.psm1 @@ -228,6 +228,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 b/source/DSCResources/DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 index 8319340c5..ec5cd99cc 100644 --- a/source/DSCResources/DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 +++ b/source/DSCResources/DSC_SqlAgentOperator/DSC_SqlAgentOperator.psm1 @@ -257,6 +257,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlAlias/DSC_SqlAlias.psm1 b/source/DSCResources/DSC_SqlAlias/DSC_SqlAlias.psm1 index dc73f9952..00fc7c22c 100644 --- a/source/DSCResources/DSC_SqlAlias/DSC_SqlAlias.psm1 +++ b/source/DSCResources/DSC_SqlAlias/DSC_SqlAlias.psm1 @@ -15,6 +15,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource since the resource modifies registry')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -130,6 +131,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource since the resource modifies registry')] [CmdletBinding()] param ( @@ -261,6 +263,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource since the resource modifies registry')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 b/source/DSCResources/DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 index 09c71d45b..bc59e75d4 100644 --- a/source/DSCResources/DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 +++ b/source/DSCResources/DSC_SqlAlwaysOnService/DSC_SqlAlwaysOnService.psm1 @@ -205,6 +205,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 b/source/DSCResources/DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 index 2c86eefda..859dd7a4a 100644 --- a/source/DSCResources/DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 +++ b/source/DSCResources/DSC_SqlConfiguration/DSC_SqlConfiguration.psm1 @@ -219,6 +219,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 b/source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 index dae2b8797..1bcf77b6a 100644 --- a/source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 +++ b/source/DSCResources/DSC_SqlDatabase/DSC_SqlDatabase.psm1 @@ -399,6 +399,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlDatabaseDefaultLocation/DSC_SqlDatabaseDefaultLocation.psm1 b/source/DSCResources/DSC_SqlDatabaseDefaultLocation/DSC_SqlDatabaseDefaultLocation.psm1 index 11168a3c4..4aab2b284 100644 --- a/source/DSCResources/DSC_SqlDatabaseDefaultLocation/DSC_SqlDatabaseDefaultLocation.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseDefaultLocation/DSC_SqlDatabaseDefaultLocation.psm1 @@ -244,6 +244,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 b/source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 index b9def2726..5906c783a 100644 --- a/source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseMail/DSC_SqlDatabaseMail.psm1 @@ -628,6 +628,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1 b/source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1 index 51e72dea5..e5289c914 100644 --- a/source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseObjectPermission/DSC_SqlDatabaseObjectPermission.psm1 @@ -49,6 +49,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-DatabaseObject is called')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -237,6 +238,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql will be implicitly called in the call to Compare-TargetResourceState')] [CmdletBinding()] param ( @@ -518,6 +520,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql will be implicitly called in the call to Compare-TargetResourceState')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlDatabasePermission/DSC_SqlDatabasePermission.psm1 b/source/DSCResources/DSC_SqlDatabasePermission/DSC_SqlDatabasePermission.psm1 index f2727e665..89cce76f5 100644 --- a/source/DSCResources/DSC_SqlDatabasePermission/DSC_SqlDatabasePermission.psm1 +++ b/source/DSCResources/DSC_SqlDatabasePermission/DSC_SqlDatabasePermission.psm1 @@ -330,6 +330,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 b/source/DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 index 2e2002ad0..9f1101262 100644 --- a/source/DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseRole/DSC_SqlDatabaseRole.psm1 @@ -408,6 +408,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 b/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 index a8c323825..8cab89a80 100644 --- a/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 +++ b/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 @@ -484,6 +484,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 b/source/DSCResources/DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 index df647bf94..bbc52d3d6 100644 --- a/source/DSCResources/DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 +++ b/source/DSCResources/DSC_SqlEndpointPermission/DSC_SqlEndpointPermission.psm1 @@ -234,6 +234,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 b/source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 index 2e8c846b8..03391f509 100644 --- a/source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 +++ b/source/DSCResources/DSC_SqlMaxDop/DSC_SqlMaxDop.psm1 @@ -215,6 +215,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 b/source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 index baeff860b..d1553ac5c 100644 --- a/source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 +++ b/source/DSCResources/DSC_SqlMemory/DSC_SqlMemory.psm1 @@ -248,6 +248,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlPermission/DSC_SqlPermission.psm1 b/source/DSCResources/DSC_SqlPermission/DSC_SqlPermission.psm1 index 624e03770..4d7d0e4a8 100644 --- a/source/DSCResources/DSC_SqlPermission/DSC_SqlPermission.psm1 +++ b/source/DSCResources/DSC_SqlPermission/DSC_SqlPermission.psm1 @@ -215,6 +215,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 b/source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 index fa35a994e..f97d5cc25 100644 --- a/source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 +++ b/source/DSCResources/DSC_SqlProtocol/DSC_SqlProtocol.psm1 @@ -186,6 +186,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Compare-TargetResourceState')] [CmdletBinding()] param ( @@ -414,6 +415,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Compare-TargetResourceState')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 index 0973e55d0..fa7183d1a 100644 --- a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 +++ b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 @@ -21,6 +21,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this function since it uses CIM methods when calling Get-ReportingServicesData')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -258,7 +259,6 @@ function Set-TargetResource $reportingServicesServiceName = 'SQLServerReportingServices' $reportingServicesDatabaseName = 'ReportServer' - } elseif ( $InstanceName -eq 'MSSQLSERVER' ) { @@ -776,6 +776,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this function since it uses CIM methods implicitly when calling Get-TargetResource')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlRSSetup/DSC_SqlRSSetup.psm1 b/source/DSCResources/DSC_SqlRSSetup/DSC_SqlRSSetup.psm1 index 85007ea57..313ff1cf3 100644 --- a/source/DSCResources/DSC_SqlRSSetup/DSC_SqlRSSetup.psm1 +++ b/source/DSCResources/DSC_SqlRSSetup/DSC_SqlRSSetup.psm1 @@ -41,6 +41,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -215,6 +216,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] <# Suppressing this rule because $global:DSCMachineStatus is used to trigger a Restart, either by force or when there are pending changes. @@ -605,6 +607,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlReplication/DSC_SqlReplication.psm1 b/source/DSCResources/DSC_SqlReplication/DSC_SqlReplication.psm1 index 029098a45..cf5986378 100644 --- a/source/DSCResources/DSC_SqlReplication/DSC_SqlReplication.psm1 +++ b/source/DSCResources/DSC_SqlReplication/DSC_SqlReplication.psm1 @@ -46,6 +46,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='This resource explicitly loads assemblies from the GAC. This is being tracked in issue https://github.com/dsccommunity/SqlServerDsc/issues/1352')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -183,6 +184,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='This resource explicitly loads assemblies from the GAC. This is being tracked in issue https://github.com/dsccommunity/SqlServerDsc/issues/1352')] [CmdletBinding()] param ( @@ -345,6 +347,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='This resource explicitly loads assemblies from the GAC. This is being tracked in issue https://github.com/dsccommunity/SqlServerDsc/issues/1352')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 b/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 index c55c1fda8..8e5304abe 100644 --- a/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 +++ b/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 @@ -327,6 +327,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlScript/DSC_SqlScript.psm1 b/source/DSCResources/DSC_SqlScript/DSC_SqlScript.psm1 index 39d22c0cd..77a60ec75 100644 --- a/source/DSCResources/DSC_SqlScript/DSC_SqlScript.psm1 +++ b/source/DSCResources/DSC_SqlScript/DSC_SqlScript.psm1 @@ -53,6 +53,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -181,6 +182,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')] [CmdletBinding()] param ( @@ -291,6 +293,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlScriptQuery/DSC_SqlScriptQuery.psm1 b/source/DSCResources/DSC_SqlScriptQuery/DSC_SqlScriptQuery.psm1 index e49b9943f..e44bb4b3e 100644 --- a/source/DSCResources/DSC_SqlScriptQuery/DSC_SqlScriptQuery.psm1 +++ b/source/DSCResources/DSC_SqlScriptQuery/DSC_SqlScriptQuery.psm1 @@ -53,6 +53,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -181,6 +182,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')] [CmdletBinding()] param ( @@ -292,6 +294,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called in Invoke-SqlScript')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 b/source/DSCResources/DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 index 9c5ee53b4..3a05c3119 100644 --- a/source/DSCResources/DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 +++ b/source/DSCResources/DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 @@ -32,6 +32,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -194,6 +195,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] [CmdletBinding()] param ( @@ -313,6 +315,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlServiceAccount/DSC_SqlServiceAccount.psm1 b/source/DSCResources/DSC_SqlServiceAccount/DSC_SqlServiceAccount.psm1 index 69fc64eda..0b49fe614 100644 --- a/source/DSCResources/DSC_SqlServiceAccount/DSC_SqlServiceAccount.psm1 +++ b/source/DSCResources/DSC_SqlServiceAccount/DSC_SqlServiceAccount.psm1 @@ -35,6 +35,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is called when Get-ServiceObject is called')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -126,6 +127,7 @@ function Get-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Get-TargetResource')] [CmdletBinding()] [OutputType([System.Boolean])] param @@ -207,6 +209,7 @@ function Test-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is called when Get-ServiceObject is called')] [CmdletBinding()] param ( diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 index e4e9237bb..ec3d4cf47 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 @@ -48,6 +48,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called implicitly in several function, for example Get-SqlEngineProperties')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -1864,6 +1865,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is implicitly called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 b/source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 index a97c927e7..1364568e6 100644 --- a/source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 +++ b/source/DSCResources/DSC_SqlWaitForAG/DSC_SqlWaitForAG.psm1 @@ -148,6 +148,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] param ( @@ -244,6 +245,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 b/source/DSCResources/DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 index 8441e4b3b..b722aeeca 100644 --- a/source/DSCResources/DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 +++ b/source/DSCResources/DSC_SqlWindowsFirewall/DSC_SqlWindowsFirewall.psm1 @@ -26,6 +26,7 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' #> function Get-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -345,6 +346,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] [CmdletBinding()] param ( @@ -634,6 +636,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='Neither command is needed for this resource')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 b/source/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 index d55918c09..a3c5eeb05 100644 --- a/source/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 +++ b/source/DSCResources/MSFT_SqlDatabaseOwner/MSFT_SqlDatabaseOwner.psm1 @@ -194,6 +194,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/MSFT_SqlDatabaseRecoveryModel/MSFT_SqlDatabaseRecoveryModel.psm1 b/source/DSCResources/MSFT_SqlDatabaseRecoveryModel/MSFT_SqlDatabaseRecoveryModel.psm1 index e07f65dd9..4d9c4647f 100644 --- a/source/DSCResources/MSFT_SqlDatabaseRecoveryModel/MSFT_SqlDatabaseRecoveryModel.psm1 +++ b/source/DSCResources/MSFT_SqlDatabaseRecoveryModel/MSFT_SqlDatabaseRecoveryModel.psm1 @@ -179,6 +179,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/MSFT_SqlServerEndpointState/MSFT_SqlServerEndpointState.psm1 b/source/DSCResources/MSFT_SqlServerEndpointState/MSFT_SqlServerEndpointState.psm1 index a5476cb64..bc200e7f7 100644 --- a/source/DSCResources/MSFT_SqlServerEndpointState/MSFT_SqlServerEndpointState.psm1 +++ b/source/DSCResources/MSFT_SqlServerEndpointState/MSFT_SqlServerEndpointState.psm1 @@ -186,6 +186,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param diff --git a/source/DSCResources/MSFT_SqlServerNetwork/MSFT_SqlServerNetwork.psm1 b/source/DSCResources/MSFT_SqlServerNetwork/MSFT_SqlServerNetwork.psm1 index e51993faa..d72237a95 100644 --- a/source/DSCResources/MSFT_SqlServerNetwork/MSFT_SqlServerNetwork.psm1 +++ b/source/DSCResources/MSFT_SqlServerNetwork/MSFT_SqlServerNetwork.psm1 @@ -100,6 +100,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is called when Get-TargetResource is called')] [CmdletBinding()] param ( @@ -289,6 +290,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param From ee41e440cfc46ecaa54ae951bb48704698d92bff Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 14:32:46 +0100 Subject: [PATCH 19/25] Rename test --- tests/QA/ScriptAnalyzer.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QA/ScriptAnalyzer.Tests.ps1 b/tests/QA/ScriptAnalyzer.Tests.ps1 index 379523aba..c2c96ee2e 100644 --- a/tests/QA/ScriptAnalyzer.Tests.ps1 +++ b/tests/QA/ScriptAnalyzer.Tests.ps1 @@ -18,7 +18,7 @@ Describe 'Script Analyzer Rules' { $scriptAnalyzerSettingsPath = Join-Path -Path $repositoryPath -ChildPath '.vscode\analyzersettings.psd1' } - Context 'When there are module files' { + Context 'When there are source files' { BeforeAll { $moduleFiles = Get-ChildItem -Path $sourcePath -Recurse -Include @('*.psm1', '*.ps1') From 612b3e230c7712d4315db529def88a74be3eefca Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 14:45:03 +0100 Subject: [PATCH 20/25] Fix rule error message --- tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 | 2 +- tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 index e44eaa1d4..62c52dbd9 100644 --- a/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 +++ b/tests/QA/AnalyzerRules/SqlServerDsc.AnalyzerRules.psm1 @@ -64,7 +64,7 @@ function Measure-CommandsNeededToLoadSMO # 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'' and add a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.' + $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 -as $diagnosticRecordType } } diff --git a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 index 2c945c594..c41fe4e60 100644 --- a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 @@ -61,7 +61,7 @@ function Get-AstFromDefinition Describe 'Measure-CommandsNeededToLoadSMO' { BeforeAll { - $expectedErrorRecordMessage = 'The function is not calling Import-SQLPSModule or Connect-SQL. If it is meant not to, then suppress the rule ''SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO'' and add a justification. See https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules for more information.' + $expectedErrorRecordMessage = '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.' } Context 'When calling the function directly' { From a30e12ce4e22bccc5646e0dd8fed17d63c08539b Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 14:45:08 +0100 Subject: [PATCH 21/25] Fix lint errors --- .../DSC_SqlDatabaseUser.psm1 | 8 ++++---- .../DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 | 2 +- .../DSC_SqlProtocolTcpIp.psm1 | 8 +++++--- .../DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 | 1 - .../DSC_SqlSecureConnection.psm1 | 1 - .../DSC_SqlSetup/DSC_SqlSetup.psm1 | 19 +++++++++---------- ...ServiceBrokerEndpointWithDefaultValues.ps1 | 1 - ...eplicasEachWithDifferentServiceAccount.ps1 | 4 +++- ...eplicasEachWithDifferentServiceAccount.ps1 | 4 +++- .../SqlScript/3-RunScriptCompleteExample.ps1 | 2 ++ 10 files changed, 27 insertions(+), 23 deletions(-) diff --git a/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 b/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 index fdf0a3b32..23d84981b 100644 --- a/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 +++ b/source/DSCResources/DSC_SqlDatabaseUser/DSC_SqlDatabaseUser.psm1 @@ -145,6 +145,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] param ( @@ -310,7 +311,6 @@ function Set-TargetResource $recreateDatabaseUser = $true } - } } @@ -444,6 +444,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Connect-Sql is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param @@ -600,15 +601,14 @@ function ConvertTo-UserType 'NoLogin' } - Default + default { $LoginType } } - } - Default + default { $errorMessage = $script:localizedData.UnknownAuthenticationType -f $AuthenticationType, $LoginType New-InvalidOperationException -Message $errorMessage diff --git a/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 b/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 index 8cab89a80..332a9adfd 100644 --- a/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 +++ b/source/DSCResources/DSC_SqlEndpoint/DSC_SqlEndpoint.psm1 @@ -571,7 +571,7 @@ function Test-TargetResource } } - if ($getTargetResourceResult.EndpointType -in @('DatabaseMirroring','ServiceBroker')) + if ($getTargetResourceResult.EndpointType -in @('DatabaseMirroring', 'ServiceBroker')) { if ($PSBoundParameters.ContainsKey('IsMessageForwardingEnabled')) { diff --git a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 index 9f870950a..94af909f3 100644 --- a/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 +++ b/source/DSCResources/DSC_SqlProtocolTcpIp/DSC_SqlProtocolTcpIp.psm1 @@ -153,7 +153,7 @@ function Get-TargetResource #> } - Default + default { $returnValue.AddressFamily = $ipAddressGroupObject.IPAddress.AddressFamily $returnValue.IpAddress = $ipAddressGroupObject.IPAddress.IPAddressToString @@ -229,6 +229,7 @@ function Get-TargetResource #> function Set-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Compare-TargetResourceState')] [CmdletBinding()] param ( @@ -359,7 +360,7 @@ function Set-TargetResource #> } - Default + default { # Check if Enable property need updating. if ($propertiesNotInDesiredState.Where( { $_.ParameterName -eq 'Enabled' })) @@ -492,6 +493,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSModule is implicitly called when calling Compare-TargetResourceState')] [CmdletBinding()] [OutputType([System.Boolean])] param @@ -704,7 +706,7 @@ function Compare-TargetResourceState ) } - Default + default { $propertiesToEvaluate = @( 'Enabled' diff --git a/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 b/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 index 8e5304abe..85877b381 100644 --- a/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 +++ b/source/DSCResources/DSC_SqlRole/DSC_SqlRole.psm1 @@ -456,7 +456,6 @@ function Test-TargetResource $isServerRoleInDesiredState = $false } } - } if ($MembersToExclude) diff --git a/source/DSCResources/DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 b/source/DSCResources/DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 index 3a05c3119..2c10c27fa 100644 --- a/source/DSCResources/DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 +++ b/source/DSCResources/DSC_SqlSecureConnection/DSC_SqlSecureConnection.psm1 @@ -119,7 +119,6 @@ function Get-TargetResource $script:localizedData.CertificateSettings ` -f 'Not Configured' ) - } } else diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 index ec3d4cf47..11184bfaf 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 @@ -1512,7 +1512,6 @@ function Set-TargetResource { $setupArgumentValue = '"{0}"' -f $currentSetupArgument.Value } - } } @@ -2181,17 +2180,17 @@ function Test-TargetResource { Write-Verbose -Message $script:localizedData.EvaluatingClusterParameters - $boundParameters.Keys | - Where-Object -FilterScript { $_ -imatch "^FailoverCluster" } | - ForEach-Object -Process { - $variableName = $_ + $variableNames = $boundParameters.Keys | + Where-Object -FilterScript { $_ -imatch "^FailoverCluster" } - if ($getTargetResourceResult.$variableName -ne $boundParameters[$variableName]) - { - Write-Verbose -Message ($script:localizedData.ClusterParameterIsNotInDesiredState -f $variableName, $($boundParameters[$variableName])) - $result = $false - } + foreach ($variableName in $variableNames) + { + if ($getTargetResourceResult.$variableName -ne $boundParameters[$variableName]) + { + Write-Verbose -Message ($script:localizedData.ClusterParameterIsNotInDesiredState -f $variableName, $($boundParameters[$variableName])) + $result = $false } + } } if ($getTargetResourceParameters.Action -eq 'Upgrade') diff --git a/source/Examples/Resources/SqlEndpoint/3-CreateServiceBrokerEndpointWithDefaultValues.ps1 b/source/Examples/Resources/SqlEndpoint/3-CreateServiceBrokerEndpointWithDefaultValues.ps1 index 3f84c5d4e..e5cd01391 100644 --- a/source/Examples/Resources/SqlEndpoint/3-CreateServiceBrokerEndpointWithDefaultValues.ps1 +++ b/source/Examples/Resources/SqlEndpoint/3-CreateServiceBrokerEndpointWithDefaultValues.ps1 @@ -34,7 +34,6 @@ Configuration Example MessageForwardingSize = 2 PsDscRunAsCredential = $SqlAdministratorCredential - } } } diff --git a/source/Examples/Resources/SqlEndpointPermission/3-AddConnectPermissionToTwoReplicasEachWithDifferentServiceAccount.ps1 b/source/Examples/Resources/SqlEndpointPermission/3-AddConnectPermissionToTwoReplicasEachWithDifferentServiceAccount.ps1 index b3d1ea081..664114d25 100644 --- a/source/Examples/Resources/SqlEndpointPermission/3-AddConnectPermissionToTwoReplicasEachWithDifferentServiceAccount.ps1 +++ b/source/Examples/Resources/SqlEndpointPermission/3-AddConnectPermissionToTwoReplicasEachWithDifferentServiceAccount.ps1 @@ -4,6 +4,8 @@ replica and an Always On secondary replica, and where each replica has a different SQL service account. #> +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='The variable $ConfigurationData is used by the HQRM test')] +param () $ConfigurationData = @{ AllNodes = @( @@ -46,7 +48,7 @@ Configuration Example Import-DscResource -ModuleName 'SqlServerDsc' - node $AllNodes.Where{$_.Role -eq 'PrimaryReplica' }.NodeName + node $AllNodes.Where{ $_.Role -eq 'PrimaryReplica' }.NodeName { SqlEndpointPermission 'SQLConfigureEndpointPermissionPrimary' { diff --git a/source/Examples/Resources/SqlEndpointPermission/4-RemoveConnectPermissionForTwoReplicasEachWithDifferentServiceAccount.ps1 b/source/Examples/Resources/SqlEndpointPermission/4-RemoveConnectPermissionForTwoReplicasEachWithDifferentServiceAccount.ps1 index f076559fb..b76510637 100644 --- a/source/Examples/Resources/SqlEndpointPermission/4-RemoveConnectPermissionForTwoReplicasEachWithDifferentServiceAccount.ps1 +++ b/source/Examples/Resources/SqlEndpointPermission/4-RemoveConnectPermissionForTwoReplicasEachWithDifferentServiceAccount.ps1 @@ -3,6 +3,8 @@ This example will remove connect permission to both an Always On primary replica and an Always On secondary replica, and where each replica has a different SQL service account. #> +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='The variable $ConfigurationData is used by the HQRM test')] +param () $ConfigurationData = @{ AllNodes = @( @@ -45,7 +47,7 @@ Configuration Example Import-DscResource -ModuleName 'SqlServerDsc' - node $AllNodes.Where{$_.Role -eq 'PrimaryReplica' }.NodeName + node $AllNodes.Where{ $_.Role -eq 'PrimaryReplica' }.NodeName { SqlEndpointPermission 'RemoveSQLConfigureEndpointPermissionPrimary' { diff --git a/source/Examples/Resources/SqlScript/3-RunScriptCompleteExample.ps1 b/source/Examples/Resources/SqlScript/3-RunScriptCompleteExample.ps1 index 67c798239..e190958cb 100644 --- a/source/Examples/Resources/SqlScript/3-RunScriptCompleteExample.ps1 +++ b/source/Examples/Resources/SqlScript/3-RunScriptCompleteExample.ps1 @@ -3,6 +3,8 @@ This example shows one way to create the SQL script files and how to run those files. #> +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='The variable $ConfigurationData is used by the HQRM test')] +param () $ConfigurationData = @{ AllNodes = @( From 5bbeaa2d55aea137c540eef81ae1a1cbf6a93753 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 14:54:10 +0100 Subject: [PATCH 22/25] Fix lint error --- source/DSCResources/DSC_SqlTraceFlag/DSC_SqlTraceFlag.psm1 | 1 + 1 file changed, 1 insertion(+) diff --git a/source/DSCResources/DSC_SqlTraceFlag/DSC_SqlTraceFlag.psm1 b/source/DSCResources/DSC_SqlTraceFlag/DSC_SqlTraceFlag.psm1 index 63e3b61df..f8409926f 100644 --- a/source/DSCResources/DSC_SqlTraceFlag/DSC_SqlTraceFlag.psm1 +++ b/source/DSCResources/DSC_SqlTraceFlag/DSC_SqlTraceFlag.psm1 @@ -299,6 +299,7 @@ function Set-TargetResource #> function Test-TargetResource { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO', '', Justification='The command Import-SQLPSMOdule is called when Get-TargetResource is called')] [CmdletBinding()] [OutputType([System.Boolean])] param From 37b0ac19aa4bdc3265de6b5dc31934628093dcd5 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 15:04:20 +0100 Subject: [PATCH 23/25] Remove debug file --- debug.ps1 | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 debug.ps1 diff --git a/debug.ps1 b/debug.ps1 deleted file mode 100644 index 24004d248..000000000 --- a/debug.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -cd C:\source\HelpUsers\johlju\SqlServerDsc -Invoke-ScriptAnalyzer ` - -Path .\output\SqlServerDsc\15.0.1\DSCResources\DSC_SqlTraceFlag\*.psm1 ` - -CustomRulePath .\tests\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1 ` - -IncludeRule @('Measure-*') - -Invoke-ScriptAnalyzer ` - -CustomRulePath @( - '.\output\RequiredModules\DscResource.AnalyzerRules' - '.\tests\QA\AnalyzerRules\SqlServerDsc.AnalyzerRules.psm1' - ) ` - -IncludeRule @('Measure-*') ` - -ScriptDefinition @' -function Get-Something {} - -function Get-TargetResource -{ - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param () - - Get-Something - - return @{} -} -'@ From b91e98e7e73f5c2d51cebddfa2316cac7c274607 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Jan 2021 15:04:28 +0100 Subject: [PATCH 24/25] Update CHANGELOG.md --- CHANGELOG.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13930b0f4..357742346 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - SqlServerDsc - - A new script analyzer rule to verify that `Import-SQLPSModule` or `Connect-SQL` + - Added a new script analyzer rule to verify that `Import-SQLPSModule` or `Connect-SQL` (that implicitly calls `Import-SQLPSModule`) is present in each `Get-`, `Test-`, and `Set-TargetResource` function. If neither command is not needed then the analyzer rule should be overridden ([issue #1683](https://github.com/dsccommunity/SqlServerDsc/issues/1683)). - - Add a new pipeline job that runs Script Analyzer on all PowerShell scripts + - Added a new pipeline job that runs Script Analyzer on all PowerShell scripts in the source folder. The rules are defined by the Script Analyzer settings file `.vscode\analyzersettings.psd1` (which also the Visual Studio Code PowerShell extension uses). @@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- SqlServerDsc + - Suppressed new custom Script Analyzer rule `SqlServerDsc.AnalyzerRules\Measure-CommandsNeededToLoadSMO` + for `Get-`, `Test-`, and `Set-TargetResource` functions in the resources. - SqlLogin - Added functionality to throw exception if an update to the `LoginMustChangePassword` value on an existing SQL Login is attempted. This functionality is not supported @@ -38,6 +41,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 of these properties that have to transition through an invalid combination (e.g. where `PasswordExpirationEnabled` is `$true` but `PasswordPolicyEnforced` is `$false`). +- SqlSetup + - Minor refactor due to source code lint errors. The loop what evaluates + the configuration parameters `*FailoverCluster` was change to a `foreach()`. ### Fixed @@ -46,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 configured when the repository moved to the new default branch `main`. It no longer throws an error when using newer versions of GitVersion ([issue #1674](https://github.com/dsccommunity/SqlServerDsc/issues/1674)). + - Minor lint errors throughout the repository. - SqlLogin - Added integration tests to assert `LoginPasswordExpirationEnabled`, `LoginPasswordPolicyEnforced` and `LoginMustChangePassword` properties/parameters From 40b5af67d5576c95800ffe4b5736c5c2d17835bb Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sun, 31 Jan 2021 08:27:55 +0100 Subject: [PATCH 25/25] Fix AnalyzerRules unit test --- tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 index c41fe4e60..1d3172660 100644 --- a/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 +++ b/tests/Unit/SqlServerDsc.AnalyzerRules.Tests.ps1 @@ -61,6 +61,12 @@ function Get-AstFromDefinition Describe 'Measure-CommandsNeededToLoadSMO' { BeforeAll { + <# + Must import the PSScriptAnalyzer module so the the tests can use the type + [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord] + #> + Import-Module -Name 'PSScriptAnalyzer' + $expectedErrorRecordMessage = '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.' }