Skip to content

AvoidUsingPositionalParameters : This rule only fires for the cmdlets / functions loaded in the default runspace #893

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

Closed
LaurentDardenne opened this issue Feb 17, 2018 · 1 comment · Fixed by #963

Comments

@LaurentDardenne
Copy link

Same behavior that this issue #883

Steps to reproduce

 $sb={
  Function Get-MyCommand {
   param( 
    [Parameter(Mandatory=$true,Position=1)] 
   $A,
   
    [Parameter(Position=2)]
   $B,
   
    [Parameter(Position=3)]
   $C
  )
  "Test"
  }
  Get-MyCommand Get-ChildItem Microsoft.PowerShell.Management System.Management.Automation.Cmdlet
 }
 Invoke-ScriptAnalyzer -ScriptDefinition "$sb"

Expected behavior

The rule is triggered.

Actual behavior

This rule does not fire because it only calls the GetCommandInfo() method.

Environment data

> $PSVersionTable
PSVersion                      5.1.14409.1012
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1012}
BuildVersion                   10.0.14409.1012
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1



> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.16.1
1.16.0
@kalgiz
Copy link
Contributor

kalgiz commented Mar 31, 2018

This happens because the script is only statically analyzed and wasn't run, so the function definition didn't appear in the Powershell runspace. Current code before checking for the positional parameters, checks if the command exists and in case it doesn't, finishes run without returning any Diagnostics Records.

Probably we should add the function definition to some dictionary during script parsing, but the big complication in here are nested function definition and analyse of the function's scope.

An easy solution that we can come up with pretty quickly is skipping the check for function existence. If the expression looks like a command with at least 3 parameters and does not use -Parameter pattern we can return some Diagnostic records with information, that we don't recognize the command as a built-in function, but if it is a function then it uses positional parameters which should be avoided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants