★ wanayoo — archive 1999 https://github.com/PowerShell/PSScriptAnalyzer/issues/1504Nouvelle recherche | Portail wanayoo
Skip to content
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

PSUseUsingScopeModifierInNewRunspaces has false positive when using -ArgumentList on Invoke-Command #1504

Open
jegannathanmaniganadan opened this issue May 20, 2020 · 5 comments

Comments

@jegannathanmaniganadan
Copy link
Contributor

@jegannathanmaniganadan jegannathanmaniganadan commented May 20, 2020 •

Steps to reproduce

 # $sb = {
Invoke-Command -Session $psSession -ArgumentList $path -ErrorAction Stop -ScriptBlock {
    Param ($Foo)

    return $Foo
}}

 # Invoke-ScriptAnalyzer -ScriptDefinition [scriptblock]$sb | ft -a

RuleName                              Severity ScriptName Line Message
--------                              -------- ---------- ---- -------
PSUseUsingScopeModifierInNewRunspaces Warning             3    The variable '$Foo' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier.
PSUseUsingScopeModifierInNewRunspaces Warning             5    The variable '$Foo' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier.

Expected behavior

$Foo should not get flagged

Actual behavior

$Foo is being flagged violating PSUseUsingScopeModifierInNewRunspaces.

Environment data

# $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
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

@mattpwhite

@bergmeister
Copy link
Collaborator

@bergmeister bergmeister commented May 20, 2020 •

I agree this is a false positive. If the ArgumentList parameter is used and there is a Param block, then PSSA should not warn for variables in the Param block. WDYT @Jawz84 ?

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 $using: pattern as the rule recommends:

$sb = {
    Invoke-Command -Session $psSession -ErrorAction Stop -ScriptBlock {
        return $using:path 
    }
}

Invoke-ScriptAnalyzer -ScriptDefinition $sb.ToString()
@Jawz84
Copy link
Contributor

@Jawz84 Jawz84 commented May 20, 2020 •

Nice find @manigandan-jegannathan-developer. This is a false positive, agreed. @bergmeister showed a work around.
I am in the middle of moving/reconstruction /w my new house, so it will be some time before I could start working on this.

@Jawz84
Copy link
Contributor

@Jawz84 Jawz84 commented May 20, 2020 •

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 Start-Job.
ForEach-Parallel does not support the -ArgumentList parameter in the -Parallel parameter set, so the problem does not arise, as the param() block just makes no sense there.
It's probably not too hard to fix. Would need to look at the code tho.

@jegannathanmaniganadan
Copy link
Contributor Author

@jegannathanmaniganadan jegannathanmaniganadan commented May 20, 2020

If the ArgumentList parameter is used and there is a Param block

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 ?

@bergmeister bergmeister changed the title PSUseUsingScopeModifierInNewRunspaces has false positives PSUseUsingScopeModifierInNewRunspaces has false positive when passing -ArgumentList to Invoke-Command May 20, 2020
@bergmeister bergmeister changed the title PSUseUsingScopeModifierInNewRunspaces has false positive when passing -ArgumentList to Invoke-Command PSUseUsingScopeModifierInNewRunspaces has false positive when using -ArgumentList on Invoke-Command May 20, 2020
@rjmholt
Copy link
Member

@rjmholt rjmholt commented May 22, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.