Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upPSUseUsingScopeModifierInNewRunspaces has false positive when using -ArgumentList on Invoke-Command #1504
Comments
|
I agree this is a false positive. If the For the moment I suggest you either suppress the warning for this specific case $sb = {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseUsingScopeModifierInNewRunspaces', '', Justification = 'Using ArgumentList')]
Param()
Invoke-Command -Session $psSession -ArgumentList $path -ErrorAction Stop -ScriptBlock {
Param ($Foo)
return $Foo
}
}
Invoke-ScriptAnalyzer -ScriptDefinition $sb.ToString()Or alternatively you could use the $sb = {
Invoke-Command -Session $psSession -ErrorAction Stop -ScriptBlock {
return $using:path
}
}
Invoke-ScriptAnalyzer -ScriptDefinition $sb.ToString() |
|
Nice find @manigandan-jegannathan-developer. This is a false positive, agreed. @bergmeister showed a work around. |
|
I've seen that this problem also arises in this example: $sb = {Start-ThreadJob -ScriptBlock {param($foo) $foo} -ArgumentList 1|wait-job|receive-job}
Invoke-ScriptAnalyzer -ScriptDefinition $sb.tostring()And likewise for |
Or you can find whether the parameter is from Param block of the immediate script block. If so then do not warn. Would not that work ? |
|
Rather than look at the invoking command, I would just build a dictionary from the param block of parameters to ignore when visiting a scriptblock |
Steps to reproduce
Expected behavior
$Foo should not get flagged
Actual behavior
$Foo is being flagged violating PSUseUsingScopeModifierInNewRunspaces.
Environment data
@mattpwhite