diff --git a/Rules/UseCompatibleCmdlets.cs b/Rules/UseCompatibleCmdlets.cs index ea96334da..c7df33764 100644 --- a/Rules/UseCompatibleCmdlets.cs +++ b/Rules/UseCompatibleCmdlets.cs @@ -43,6 +43,7 @@ private struct RuleParameters private bool hasInitializationError; private string reference; private readonly string defaultReference = "desktop-5.1.14393.206-windows"; + private readonly string alternativeDefaultReference = "core-6.0.2-windows"; private RuleParameters ruleParameters; public UseCompatibleCmdlets() @@ -274,6 +275,10 @@ private void SetupCmdletsDictionary() ruleParameters.compatibility = compatibilityList.ToArray(); reference = defaultReference; + if (compatibilityList.Count == 1 && compatibilityList[0] == defaultReference) + { + reference = alternativeDefaultReference; + } #if DEBUG // Setup reference file object referenceObject; @@ -326,7 +331,7 @@ private void SetupCmdletsDictionary() return; } - var extentedCompatibilityList = compatibilityList.Concat(Enumerable.Repeat(reference, 1)); + var extentedCompatibilityList = compatibilityList.Union(Enumerable.Repeat(reference, 1)); foreach (var compat in extentedCompatibilityList) { string psedition, psversion, os; diff --git a/Tests/Rules/UseCompatibleCmdlets.tests.ps1 b/Tests/Rules/UseCompatibleCmdlets.tests.ps1 index 62d1e903b..ba39f1524 100644 --- a/Tests/Rules/UseCompatibleCmdlets.tests.ps1 +++ b/Tests/Rules/UseCompatibleCmdlets.tests.ps1 @@ -26,9 +26,11 @@ Describe "UseCompatibleCmdlets" { process { It ("found {0} violations for '{1}'" -f $expectedViolations, $command) { - Invoke-ScriptAnalyzer -ScriptDefinition $command -IncludeRule $ruleName -Settings $settings | ` - Get-Count | ` - Should -Be $expectedViolations + $warnings = Invoke-ScriptAnalyzer -ScriptDefinition $command -IncludeRule $ruleName -Settings $settings + $warnings.Count | Should -Be $expectedViolations + $warnings | ForEach-Object { + $_.RuleName | Should -Be 'PSUseCompatibleCmdlets' + } } } } @@ -54,4 +56,9 @@ Describe "UseCompatibleCmdlets" { @("Start-VM", "New-SmbShare", "Get-Disk") | ` Test-Command -Settings $settings -ExpectedViolations 1 } + + Context "Default reference can also be used as target platform" { + $settings = @{rules=@{PSUseCompatibleCmdlets=@{compatibility=@("desktop-5.1.14393.206-windows")}}} + @("Remove-Service") | Test-Command -Settings $settings -ExpectedViolations 1 + } }