PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.85k stars 373 forks source link

psdscuseidenticalmandatoryparametersfordsc errors #834

Open mcollera opened 6 years ago

mcollera commented 6 years ago

GET/SET/Test have identical parameters.

param ( [Parameter(Mandatory=$true)] [System.String] $DistinguishedName,

    [Parameter(Mandatory=$true)]
    [Microsoft.Management.Infrastructure.CimInstance[]]
    $AccessControlList,

    [Parameter(Mandatory=$false)]
    [bool]
    $Force = $false
)

I get this error when the rule runs.

Invoke-ScriptAnalyzer : Object reference not set to an instance of an object. At line:1 char:1

If I exclude this rule then I get no errors.

bergmeister commented 6 years ago

Your repro seems to be incomplete. Using PSScriptAnalyzer on your Param block is fine for me. Please provide more details

bergmeister commented 6 years ago

@mcollera Will close since I cannot repro and you did not respond. Feel free to re-open if you can provide an example with more details that does reproduce.

mcollera commented 6 years ago

@bergmeister I have an example that causes the error.

[ClassVersion("1.0.0.0")] class SubClass{ [Write] String Param1; [Write] String Param2; };

[ClassVersion("1.0.0.0"), FriendlyName("Embedded")] class JL_Embedded: OMI_BaseResource { [Key] String Name; [Write, EmbeddedInstance("SubClass")] string SubClass[]; };

This error is caused by the schema mof when you have two classes. If you move the main class to the top the error goes away. but that causes an error in PSScriptAnalyzer. Link bellow to that issue. https://github.com/PowerShell/xDSCResourceDesigner/issues/73

bergmeister commented 6 years ago

@mcollera Can you please give more details how you call scriptanalyzer against the ps1 file please? A full example would be helpful. If I do it locally against a file with the content that you describe, I get parser errors (sorry, I am not very familiar with DSC):

Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDependency
Invoke-ScriptAnalyzer : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing '(' in class method parameter list at line 3 column 19.
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (MissingMethodParameterList:String) [Invoke-ScriptAnalyzer], ParseException
    + FullyQualifiedErrorId : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing '(' in class method parameter list at line 3 column 19.,Microsoft.Windows.PowerShell.ScriptAnalyzer.Comm
   ands.InvokeScriptAnalyzerCommand

Invoke-ScriptAnalyzer : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing function body in function declaration at line 3 column 19.
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (MissingFunctionBody:String) [Invoke-ScriptAnalyzer], ParseException
    + FullyQualifiedErrorId : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing function body in function declaration at line 3 column 19.,Microsoft.Windows.PowerShell.ScriptAnalyzer.C
   ommands.InvokeScriptAnalyzerCommand

Invoke-ScriptAnalyzer : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing ] at end of attribute or type literal at line 7 column 25.
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (EndSquareBracketExpectedAtEndOfAttribute:String) [Invoke-ScriptAnalyzer], ParseException
    + FullyQualifiedErrorId : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing ] at end of attribute or type literal at line 7 column 25.,Microsoft.Windows.PowerShell.ScriptAnalyzer.C
   ommands.InvokeScriptAnalyzerCommand

Invoke-ScriptAnalyzer : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing expression after unary operator ',' at line 7 column 26.
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (MissingExpressionAfterOperator:String) [Invoke-ScriptAnalyzer], ParseException
    + FullyQualifiedErrorId : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing expression after unary operator ',' at line 7 column 26.,Microsoft.Windows.PowerShell.ScriptAnalyzer.Com
   mands.InvokeScriptAnalyzerCommand

Invoke-ScriptAnalyzer : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Unexpected token 'FriendlyName' in expression or statement at line 7 column 27.
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (UnexpectedToken:String) [Invoke-ScriptAnalyzer], ParseException
    + FullyQualifiedErrorId : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Unexpected token 'FriendlyName' in expression or statement at line 7 column 27.,Microsoft.Windows.PowerShell.Scr
   iptAnalyzer.Commands.InvokeScriptAnalyzerCommand

Invoke-ScriptAnalyzer : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing '(' in class method parameter list at line 10 column 17.
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (MissingMethodParameterList:String) [Invoke-ScriptAnalyzer], ParseException
    + FullyQualifiedErrorId : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing '(' in class method parameter list at line 10 column 17.,Microsoft.Windows.PowerShell.ScriptAnalyzer.Com
   mands.InvokeScriptAnalyzerCommand

Invoke-ScriptAnalyzer : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing function body in function declaration at line 10 column 17.
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (MissingFunctionBody:String) [Invoke-ScriptAnalyzer], ParseException
    + FullyQualifiedErrorId : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Missing function body in function declaration at line 10 column 17.,Microsoft.Windows.PowerShell.ScriptAnalyzer.
   Commands.InvokeScriptAnalyzerCommand

Invoke-ScriptAnalyzer : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Unexpected attribute 'ClassVersion' at line 7 column 1.
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path C:\Users\cberg\Desktop\a.ps1  -SaveDscDep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (UnexpectedAttribute:String) [Invoke-ScriptAnalyzer], ParseException
    + FullyQualifiedErrorId : Parse error in file C:\Users\cberg\Desktop\a.ps1:  Unexpected attribute 'ClassVersion' at line 7 column 1.,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.Inv
   okeScriptAnalyzerCommand
mcollera commented 6 years ago

@bergmeister Yes I have an example. Here is a resource that I wrote that gets the error. ResourceControllerdsc If you run Invoke-ScriptAnalyzer on the project it will fail the test psdscuseidenticalmandatoryparametersfordsc. The current code you download wont error but in the schema.mof file here. If you switch the order of the two classes it will cause the failure. It seems that test doesn't work when the main class is not listed last.

bergmeister commented 6 years ago

Ok, I can confirm this happens in 1.17.1, even when analysing the 2nd time (meaning that the cache would already be populated), will therefore re-open.