-
Notifications
You must be signed in to change notification settings - Fork 395
PSUseUsingScopeModifierInNewRunspaces has false positive when using -ArgumentList on Invoke-Command #1504
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
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 |
Somewhat related, the following error is also a false positive that I don't understand how it made it into a stable release of PSSA:
I don't think there's a problem with using PS: I customize the output format of PSSA a little bit, but just the EDIT: After disabling this buggy rule in my tests I instantly had 65 warnings less .......... 🙄 |
This is a false positive, agreed. You can either suppress the warning, use the $using: pattern as the rule recommends, or try this work around : $sb1 = [ScriptBlock]::Create({
Param ($Foo)
return $Foo
}
)
$sb2 = [ScriptBlock]::Create( { Invoke-Command -Session $psSession -ArgumentList $path -ErrorAction Stop -ScriptBlock $sb1 } )
Invoke-ScriptAnalyzer -ScriptDefinition $sb2 | ft -a |
Indentation and fix PSScriptAnalyzer 's [false positive](PowerShell/PSScriptAnalyzer#1504) at: ```powershell $Targets += [Target]::new($Target,( Start-Job -ScriptBlock -scriptblock { ... } -ArgumentList ```
Suppression did not work so worked around:
|
Aside from the false positive, this rule doesn't return a unique [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseUsingScopeModifierInNewRunspaces', 'Foo', Justification = 'Using ArgumentList')] |
It seems that three years later this still has not been fixed. That is not good |
Steps to reproduce
Expected behavior
$Foo should not get flagged
Actual behavior
$Foo is being flagged violating PSUseUsingScopeModifierInNewRunspaces.
Environment data
@mattpwhite
The text was updated successfully, but these errors were encountered: