The rule 'UseUsingScopeModifierInNewRunspaces' throw an ArgumentException
See original GitHub issueSteps to reproduce
$s=@'
function Get-One{
Invoke-Command -Session $sourceRemoteSession -ScriptBlock {
if($null -eq (Get-PSProvider CMSite -ea Ignore)){
$sccmModule = ("{0}\ConfigurationManager.psd1" -f (Split-Path $env:SMS_ADMIN_UI_PATH -Parent))
Import-Module "$sccmModule" -Global
}
}
return $importState
}
function Get-Two{
Invoke-Command -Session $sourceRemoteSession -ScriptBlock {
if($null -eq (Get-PSProvider CMSite -ea Ignore)){
$sccmModule = ("{0}\ConfigurationManager.psd1" -f (Split-Path $env:SMS_ADMIN_UI_PATH -Parent))
Import-Module "$sccmModule" -Global
}
}
return $importState
}
'@
Invoke-ScriptAnalyzer -ScriptDefinition $s
Expected behavior
No error
Actual behavior
Invoke-ScriptAnalyzer : Un élément avec la même clé a déjà été ajouté.
(An item with the same key has already been added)
Au caractère Ligne:1 : 1
+ Invoke-ScriptAnalyzer -ScriptDefinition $s
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [Invoke-ScriptAnalyzer], ArgumentException
+ FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *
writeErrorStream : True
PSMessageDetails :
Exception : System.ArgumentException: Un élément avec la même clé a déjà été ajouté.
à System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
à System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseUsingScopeModifierInNewRunspac
es.SyntaxCompatibilityVisitor.AddAssignedVarsToSession(String sessionName,
IReadOnlyDictionary`2 variablesToAdd)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseUsingScopeModifierInNewRunspac
es.SyntaxCompatibilityVisitor.VisitScriptBlockExpression(ScriptBlockExpressionAst
scriptBlockExpressionAst)
à System.Management.Automation.Language.ScriptBlockExpressionAst.InternalVisit(AstVisitor
visitor)
à System.Management.Automation.Language.CommandAst.InternalVisit(AstVisitor visitor)
à System.Management.Automation.Language.PipelineAst.InternalVisit(AstVisitor visitor)
à System.Management.Automation.Language.StatementBlockAst.InternalVisit(AstVisitor visitor,
ReadOnlyCollection`1 traps, ReadOnlyCollection`1 statements, AstVisitAction action)
à System.Management.Automation.Language.NamedBlockAst.InternalVisit(AstVisitor visitor)
à System.Management.Automation.Language.ScriptBlockAst.InternalVisit(AstVisitor visitor)
à System.Management.Automation.Language.FunctionDefinitionAst.InternalVisit(AstVisitor
visitor)
à System.Management.Automation.Language.StatementBlockAst.InternalVisit(AstVisitor visitor,
ReadOnlyCollection`1 traps, ReadOnlyCollection`1 statements, AstVisitAction action)
à System.Management.Automation.Language.NamedBlockAst.InternalVisit(AstVisitor visitor)
à System.Management.Automation.Language.ScriptBlockAst.InternalVisit(AstVisitor visitor)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseUsingScopeModifierInNewRunspac
es.AnalyzeScript(Ast ast, String fileName)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass83_1.<AnalyzeS
yntaxTree>b__2()
TargetObject :
CategoryInfo : InvalidOperation : (:) [Invoke-ScriptAnalyzer], ArgumentException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : à <ScriptBlock>, <Aucun fichier> : ligne 1
PipelineIterationInfo : {0, 1}
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.752
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.752}
BuildVersion 10.0.18362.752
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.19.0
1.18.3
1.18.1
1.18.0
1.17.1
The presence of the $sccmModule variable inside the two scriptblock causes the exception. If we rename the first variable occurence to $sccmModuleOne there are no more exceptions.
Once again we do not know where the error took place. In the file ‘xyz’, but for which line? In the error report, we do not know which line triggered the error, yet the analysis engine knows it.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
ArgumentException Class (System)
ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of...
Read more >Throwing ArgumentException and InvalidOperationException
An exception is thrown when an error is encountered in a running application, whether from bad code or bad user input.
Read more >How to decide whether I should throw ArgumentException ...
From the Docs: ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the...
Read more >Commonly Encountered C# Exceptions
The ArgumentException type is thrown when a method argument receives an invalid argument value. This exception type inherits from the System ...
Read more >The Case against Throwing ArgumentException and the Like
Throw ArgumentNullException when the caller has passed a null parameter to the method. But throwing an exception is only half of the story....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No worries, it’s just an FYI, I have a fix for it already in #1493
Thank you for attending to this and notifying me. I can’t help at this time. I am in the middle of refurbishing my new house where we will be moving to in a few weeks.