PowerShell / PowerShell/PSScriptAnalyzer

Settings file (.psd1) sometimes requires arrays even when specifying a single value

Open
#1,366 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Configuration Issue - Bug
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Generally, in .psd1 files - and in PowerShell as a whole - it is fine to specify a scalar where an array is ultimately bound.

This is how it works in a module manifest's FunctionsToExport entry, for instance: instead of having to explicitly specify an array for a single entry - @('Get-Foo') or , 'Get-Foo - just 'Get-Foo' (scalar) is also accepted.

This is currently not the case in PSSA, at least for the PSAvoidUsingCmdletAliases rule's Whitelist property:

A scalar value is quietly ignored.

Not sure what other properties are affected.

Steps to reproduce

Run the following Pester test:

Describe "Scalar property-value test" {
  BeforeAll {
    Push-Location TestDrive:/
    Import-Module -ErrorAction Stop PSScriptAnalyzer
  }
  It "Acceptes a scalar as a single value" {
    @'
    Rules = @{
      Rules = @{
        # Do not warn about using the '%' alias
        # !! `@('%')` or `, '%'` are currently required - scalar  '%' does NOT work.
        PSAvoidUsingCmdletAliases = @{ Whitelist = '%' }
      }
    }    
'@ > test.psd1

   @'
1, 2 | % { $_ }
'@ > test.ps1

   Invoke-ScriptAnalyzer -Settings test.psd1 test.ps1 | Should -BeNullOrEmpty

  }
  AfterAll {
    Pop-Location
  }
}

Expected behavior

The test should pass.

Actual behavior

The test fails, because a PSAvoidUsingCmdletAliases warning is still emitted, because the non-array value '%' didn't take effect.

Environment data

> $PSVersionTable
PowerShell Core v7.0.0-preview.5 (7.0.0-preview.5) on macOS 10.15.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Pester test described in issue #1366 with PSAvoidUsingCmdletAliases and Whitelist = '%'. Trace how the test.psd1 settings are read and how Whitelist is applied; done means the scalar value suppresses the alias warning and the regression test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.