Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rjmholt opened this issue Mar 1, 2019 · 3 comments · Fixed by #1161
Closed

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

rjmholt opened this issue Mar 1, 2019 · 3 comments · Fixed by #1161

Comments

@rjmholt
Copy link
Contributor

rjmholt commented Mar 1, 2019

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?

@bergmeister
Copy link
Collaborator

bergmeister commented Mar 1, 2019

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.

@rjmholt
Copy link
Contributor Author

rjmholt commented Mar 1, 2019

I've figured it out!

@rjmholt
Copy link
Contributor Author

rjmholt commented Mar 13, 2019

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants