PSUseUsingScopeModifierInNewRunspaces has false positive when using -ArgumentList on Invoke-Command
See original GitHub issueSteps 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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:8 (4 by maintainers)
Top Results From Across the Web
invoke command string with named parameters gone wrong
I want to execute a PowerShell script from Another server via Invoke-Command . When I try it the response say that there is...
Read more >Invoke-Command ScriptBlock issue
In summary I want to know how I can use variables in script block and such that command can be executed in machine...
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
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
$env:
variables on remote systems. If you want to prevent uninitialized/null variables in peoples’ scripts then just warn if they’re not doing aSet-StrictMode -Version 1.0
(or higher) in the beginning. This makes sure the script exits when it encounters an unset/uninitialized variable.PS: I customize the output format of PSSA a little bit, but just the
Location
property, this doesn’t affect the Rule processing and messages.EDIT: After disabling this buggy rule in my tests I instantly had 65 warnings less … 🙄
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