Skip to content

Unable to run rules with settings file that run fine with hashtable #1160

Closed

Description

@rjmholt

I've got a script with some compatibility bugs in it. I can run the compatibility rules on it like this:

$settings = @{
    Rules = @{
        PSUseCompatibleCommands = @{
            Enable = $true
            TargetProfiles = @(
                'win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework' # Server 2019 - PS 5.1 (the platform it already runs on)
                'win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework' # Server 2012 - PS 3
                'ubuntu_x64_18.04_6.1.3_x64_4.0.30319.42000_core' # Ubuntu 18.04 - PS 6.1
            )
        }
        PSUseCompatibleTypes = @{
            Enable = $true
            TargetProfiles = @(
                'win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework'
                'win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework'
                'ubuntu_x64_18.04_6.1.3_x64_4.0.30319.42000_core'
            )
        }
        PSUseCompatibleSyntax = @{
            Enable = $true
            TargetVersions = @('3.0', '5.1', '6.1')
        }
    }
}

Invoke-ScriptAnalyzer -Settings $settings -Path ./script.ps1

And get:


RuleName                            Severity     ScriptName Line  Message                                                     
--------                            --------     ---------- ----  -------                                                     
PSUseCompatibleCommands             Warning      blogBad.ps 9     The parameter 'FullyQualifiedName' is not available for     
                                                 1                command 'Import-Module' by default in PowerShell version    
                                                                  '3.0' on platform 'Microsoft Windows Server 2012 Datacenter'
PSUseCompatibleCommands             Warning      blogBad.ps 13    The parameter 'Depth' is not available for command          
                                                 1                'Get-ChildItem' by default in PowerShell version '3.0' on   
                                                                  platform 'Microsoft Windows Server 2012 Datacenter'         
PSUseCompatibleCommands             Warning      blogBad.ps 22    The command 'Get-AuthenticodeSignature' is not available by 
                                                 1                default in PowerShell version '6.1.3' on platform 'Ubuntu   
                                                                  18.04.2 LTS'                                                
PSUseCompatibleCommands             Warning      blogBad.ps 30    The command 'Compress-Archive' is not available by default  
                                                 1                in PowerShell version '3.0' on platform 'Microsoft Windows  
                                                                  Server 2012 Datacenter'                                     
PSUseCompatibleTypes                Warning      blogBad.ps 12    The method 'Get' is not available on type                   
                                                 1                'System.Management.Automation.WildcardPattern' by default   
                                                                  in PowerShell version '3.0' on platform 'Microsoft Windows  
                                                                  Server 2012 Datacenter'                                     
PSUseCompatibleSyntax               Warning      blogBad.ps 11    The constructor syntax '[System.Collections.Generic.List[Sys
                                                 1                tem.Management.Automation.Signature]]::new()' is not        
                                                                  available by default in PowerShell versions 3,4             
                                                                             

But when I put the settings in a psd1 file and run the same with:

Invoke-ScriptAnalyzer -Settings ./mysettings.psd1 -Path ./script.ps1

I get no output.

Is this expected behaviour -- is there something I need to add to a settings file to make this work? Or is this something I should be fixing in the engine?

Activity

  1. bergmeister commented on Mar 1, 2019

    @bergmeister
    Collaborator

    Hmm, maybe there is a small problem in the psd1 file that makes problems when parsing it. PSSA does not have a good error behaviour in this area (it fails silently and decides to not use the settings file if it cannot parse it without giving parse error details). Run it with -Verbose to see more details of where it goes wrong.

  2. rjmholt commented on Mar 1, 2019

    @rjmholt
    ContributorAuthor

    I've figured it out!

  3. rjmholt commented on Mar 13, 2019

    @rjmholt
    ContributorAuthor

    In case people are interested, arrays in PowerShell essentially are anything with , between them.

    The @(...) syntax works like a "flatten into an array" expression.

    When you don't use commas, the expression is evaluated as a statement, and its output added to the array. So

    @(1
    2
    3)
    

    is actually equivalent to

    @(1; 2; 3)
    

    rather than

    @(1, 2, 3)
    

    (The first two being an array container with three statements each emitting one output, and the last being an array container with a single statement emitting three outputs).

    So technically, allowing newlines means you don't have a statically determinable array:

    function Get-Things
    {
        for ($i = 0; $i -lt 10; $i++) { $i }
    }
    
    # This has 11 elements
    @(Get-Things, 11)

    But, people are so used to it working that it would cause problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions