Skip to content

Scan multiple times a module throw System.InvalidOperationException #900

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 20, 2018 · 2 comments
Closed

Comments

@LaurentDardenne
Copy link

Steps to reproduce

Run a scan multiple times on a module :

ipmo PSScriptAnalyzer
$res=Invoke-ScriptAnalyzer -path 'C:\Program Files\WindowsPowerShell\Modules\InvokeBuild\5.1.0'

image

Expected behavior

No error.

Actual behavior

throw System.InvalidOperationException:
Collection was modified; enumeration operation may not execute.

Environment data

$psversiontable
Name                           Value
----                           -----
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
1.15.0
1.12.0
1.11.1

Log:

[STA] C:\temp> rver

writeErrorStream      : True
PSMessageDetails      :
Exception             : System.Management.Automation.CmdletInvocationException: La collection a été modifiée;
                        l'opération d'énumération peut ne pas s'exécuter. ---> System.InvalidOperationException: La
                        collection a été modifiée; l'opération d'énumération peut ne pas s'exécuter.
                           à System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
                           à System.Collections.Generic.List`1.Enumerator.MoveNextRare()
                           à Microsoft.PowerShell.Commands.ModuleCmdletBase.UpdateCommandCollection(Collection`1 list,
                        List`1 patterns)
                           à Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String
                        moduleManifestPath, ExternalScriptInfo scriptInfo, Hashtable data, Hashtable localizedData,
                        ManifestProcessingFlags manifestProcessingFlags, Version minimumVersion, Version
                        maximumVersion, Version requiredVersion, Nullable`1 requiredModuleGuid, ImportModuleOptions&
                        options, Boolean& containedErrors)
                           à Microsoft.PowerShell.Commands.TestModuleManifestCommand.ProcessRecord()
                           à System.Management.Automation.CommandProcessor.ProcessRecord()
                           --- Fin de la trace de la pile d'exception interne ---
                           à System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
                           à System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs,
                        Boolean performSyncInvoke)
                           à System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace
                        rsToUse, Boolean isSync)
                           à
                        System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1
                        input, PSDataCollection`1 output, PSInvocationSettings settings)
                           à System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1
                        input, PSDataCollection`1 output, PSInvocationSettings settings)
                           à System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings
                        settings)
                           à Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetModuleManifest(String filePath,
                        IEnumerable`1& errorRecord)
                           à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.MissingModuleManifestField.<Analy
                        zeScript>d__0.MoveNext()
                           à System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                           à System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                           à Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass78_0.<AnalyzeS
                        yntaxTree>b__1()
TargetObject          : C:\Program Files\WindowsPowerShell\Modules\InvokeBuild\5.1.0\InvokeBuild.psd1
CategoryInfo          : InvalidOperation: (C:\Program File...nvokeBuild.psd1:String) [Invoke-ScriptAnalyzer],
                        CmdletInvocationException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}

MyCommand             : Invoke-ScriptAnalyzer
BoundParameters       : {}
UnboundArguments      : {}
ScriptLineNumber      : 1
OffsetInLine          : 6
HistoryId             : 9
ScriptName            :
Line                  : $res=Invoke-ScriptAnalyzer -path 'C:\Program
                        Files\WindowsPowerShell\Modules\InvokeBuild\5.1.0'
PositionMessage       : At line:1 char:6
                        + $res=Invoke-ScriptAnalyzer -path 'C:\Program Files\WindowsPowerShell\ ...
                        +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot          :
PSCommandPath         :
InvocationName        : Invoke-ScriptAnalyzer
PipelineLength        : 0
PipelinePosition      : 0
ExpectingInput        : False
CommandOrigin         : Internal
DisplayScriptPosition :

Exception at nesting level 0 ---------------------------------------------------

ErrorRecord                 : La collection a été modifiée ; l'opération d'énumération peut ne pas s'exécuter.
WasThrownFromThrowStatement : False
Message                     : La collection a été modifiée ; l'opération d'énumération peut ne pas s'exécuter.
Data                        : {}
InnerException              : System.InvalidOperationException: La collection a été modifiée; l'opération
                              d'énumération peut ne pas s'exécuter.
                                 à System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
                                 à System.Collections.Generic.List`1.Enumerator.MoveNextRare()
                                 à Microsoft.PowerShell.Commands.ModuleCmdletBase.UpdateCommandCollection(Collection`1
                              list, List`1 patterns)
                                 à Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String
                              moduleManifestPath, ExternalScriptInfo scriptInfo, Hashtable data, Hashtable
                              localizedData, ManifestProcessingFlags manifestProcessingFlags, Version minimumVersion,
                              Version maximumVersion, Version requiredVersion, Nullable`1 requiredModuleGuid,
                              ImportModuleOptions& options, Boolean& containedErrors)
                                 à Microsoft.PowerShell.Commands.TestModuleManifestCommand.ProcessRecord()
                                 à System.Management.Automation.CommandProcessor.ProcessRecord()
TargetSite                  : System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]
                              Invoke(System.Collections.IEnumerable)
StackTrace                  :    à System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
                                 à System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace
                              rs, Boolean performSyncInvoke)
                                 à
                              System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace
                              rsToUse, Boolean isSync)
                                 à System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataColle
                              ction`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
                                 à
                              System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1
                              input, PSDataCollection`1 output, PSInvocationSettings settings)
                                 à System.Management.Automation.PowerShell.Invoke(IEnumerable input,
                              PSInvocationSettings settings)
                                 à Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetModuleManifest(String
                              filePath, IEnumerable`1& errorRecord)
                                 à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.MissingModuleManifestField.
                              <AnalyzeScript>d__0.MoveNext()
                                 à System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                                 à System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                                 à Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass78_0.<An
                              alyzeSyntaxTree>b__1()
HelpLink                    :
Source                      : System.Management.Automation
HResult                     : -2146233087

Exception at nesting level 1 ---------------------------------------------------

Message        : La collection a été modifiée ; l'opération d'énumération peut ne pas s'exécuter.
Data           : {}
InnerException :
TargetSite     : Void ThrowInvalidOperationException(System.ExceptionResource)
StackTrace     :    à System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
                    à System.Collections.Generic.List`1.Enumerator.MoveNextRare()
                    à Microsoft.PowerShell.Commands.ModuleCmdletBase.UpdateCommandCollection(Collection`1 list, List`1
                 patterns)
                    à Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String moduleManifestPath,
                 ExternalScriptInfo scriptInfo, Hashtable data, Hashtable localizedData, ManifestProcessingFlags
                 manifestProcessingFlags, Version minimumVersion, Version maximumVersion, Version requiredVersion,
                 Nullable`1 requiredModuleGuid, ImportModuleOptions& options, Boolean& containedErrors)
                    à Microsoft.PowerShell.Commands.TestModuleManifestCommand.ProcessRecord()
                    à System.Management.Automation.CommandProcessor.ProcessRecord()
HelpLink       :
Source         : mscorlib
HResult        : -2146233079

[STA] C:\temp> $stacktrace
   à System.Management.Automation.AuthorizationManager.ShouldRunInternal(CommandInfo commandInfo, CommandOrigin origin,
PSHost host)
   à System.Management.Automation.CommandDiscovery.ShouldRun(ExecutionContext context, PSHost host, CommandInfo commandI
nfo, CommandOrigin commandOrigin)
   à System.Management.Automation.CommandDiscovery.LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin command
Origin, Nullable`1 useLocalScope, SessionStateInternal sessionState)
   à System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigi
n, Nullable`1 useLocalScope)
   à System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
   à System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandEleme
nts, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
   à System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal
[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcCon
text)
   à System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
   à System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
@LaurentDardenne
Copy link
Author

Repeated analysis on a manifesto seems to be the cause :

1..20|% { $res=Invoke-ScriptAnalyzer -path 'C:\Program Files\WindowsPowerShell\Modules\PSScriptAnalyzer\1.16.1\PSScriptAnalyzer.psm1'}
#no errror

1..20|% { $res=Invoke-ScriptAnalyzer -path 'C:\Program Files\WindowsPowerShell\Modules\PSScriptAnalyzer\1.16.1\PSScriptAnalyzer.psd1'}
# error

The error message may be different :

Invoke-ScriptAnalyzer : Le tableau de destination n'est pas assez long. Vérifiez destIndex et la longueur, ainsi que
les limites inférieures du tableau.
At line:1 char:16
+ ... 20|% { $res=Invoke-ScriptAnalyzer -path 'C:\Program Files\WindowsPowe ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (C:\Program File...ptAnalyzer.psd1:String) [Invoke-ScriptAnalyzer], Cm
   dletInvocationException
    + FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerComm
   and

#OR

Invoke-ScriptAnalyzer : L'index se trouve en dehors des limites du tableau.
At line:1 char:16
+ ... 30|% { $res=Invoke-ScriptAnalyzer -path 'C:\Program Files\WindowsPowe ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (C:\Program File...ptAnalyzer.psd1:String) [Invoke-ScriptAnalyzer], Cm
   dletInvocationException
    + FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerComm
   and

@LaurentDardenne
Copy link
Author

No error when I exclude the following rules : PSUseToExportFieldsInManifest,PSMissingModuleManifestField

1..30|% { $res=Invoke-ScriptAnalyzer -path 'C:\Program Files\Windows
PowerShell\Modules\PSScriptAnalyzer\1.16.1\PSScriptAnalyzer.psd1' -ExcludeRule PSUseToExportFieldsInManifest,PSMissingModuleManifestField}

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

No branches or pull requests

1 participant