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

Add UseUsingScopeModifierInNewRunspaces rule #1419

Merged
merged 59 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
8b9f115
Add tests for AvoidUnInitializedVarsInNewRunspaces
Feb 16, 2020
0ab5fbf
Add strings for AvoidUnInitializedVarsInNewRunspaces
Feb 16, 2020
7182edb
Add documentation
Feb 17, 2020
068b1fc
wrestling with Ast in C# - not working
Feb 17, 2020
adbeff0
Add sensible warning message
Feb 20, 2020
23183d2
remove unnecessary boilerplate from test
Feb 20, 2020
83394d6
clean up and finetune rule
Feb 20, 2020
99c2bea
Add RuleToTest parameter to Test-ScriptAnalyzer to aid with test driv…
Feb 20, 2020
91e6689
increment rule count to fix test
Feb 20, 2020
337ac20
add reference to rule documentation
Feb 20, 2020
406d7ec
exclude built-in variables
Feb 20, 2020
55b7316
change using directive => scoope modifier
Feb 24, 2020
ef4b79a
add tests for InlineScript, Invoke-Command and Start-(Thread)Job
Feb 24, 2020
36bb1fc
add rule implementation for InlineScript, Invoke-Command and Start-(T…
Feb 24, 2020
e7fd6cc
Refactor and cleanup
Feb 24, 2020
59ba18b
small cleanup
Feb 24, 2020
f64c81c
explain all applicable situations in documentation
Feb 27, 2020
db80c8f
simplify code for adding sessions to dict
Feb 27, 2020
d1f8df8
Revert "Add RuleToTest parameter to Test-ScriptAnalyzer to aid with t…
Feb 27, 2020
c53b65d
Rename rule to UseUsingScopeModifierInNewRunspaces
Feb 27, 2020
15bd608
refactor grouping of script blocks by session name
Feb 27, 2020
37a5cb1
Add suggested correction implementation
Mar 4, 2020
6024c50
Add test for suggested corrections, fix typos
Mar 7, 2020
b12d16f
Refactor to AstVisitor/AstVisitor2 WIP
Mar 7, 2020
9bc2195
Update Rules/UseUsingScopeModifierInNewRunspaces.cs
Jawz84 Mar 7, 2020
f8fa89a
Update Rules/UseUsingScopeModifierInNewRunspaces.cs
Jawz84 Mar 7, 2020
6ff3405
Process review comments
Mar 7, 2020
889b03a
Merge remote-tracking branch 'refs/remotes/origin/1410_foreach_parall…
Mar 7, 2020
6758748
Add tests for command name and icm -session
Mar 10, 2020
e5e6a58
Add icm -session logic to visitor and tidy up
Mar 10, 2020
921c1d2
fix build for windows powershell
Mar 10, 2020
f57c610
Revert "fix build for windows powershell"
Mar 11, 2020
7956eda
Change private class to internal class for Windows PowerShell
Mar 11, 2020
427461a
Add logic to detect DSCScriptResource
Mar 12, 2020
4f8bf36
Add tests for DSC Script resource
Mar 12, 2020
0cb8f9e
Enhance label test topic
Jawz84 Mar 13, 2020
af2ceee
repair test indentation and add newline at eof
Mar 13, 2020
52bece8
Merge remote-tracking branch 'refs/remotes/origin/1410_foreach_parall…
Mar 13, 2020
29176a8
Move testcases to BeforeAll blocks
Mar 13, 2020
b1bee39
Add documentation that DSC Script resource is supported
Mar 13, 2020
f6de07d
change string[] to IReadOnlyList<string>
Jawz84 Mar 13, 2020
5f6bae0
extract scriptBlockPosition as a variable for readability
Jawz84 Mar 13, 2020
bcaba83
put arguments on their own line for readability
Mar 13, 2020
1d6b55e
make visitor class a private, nested class in rule
Mar 13, 2020
92f19c9
change 'var' to type name for method calls
Mar 13, 2020
7943625
fix indentation for nested visitor class
Mar 13, 2020
a6acfd7
Remove explicit 'ToList()' for performance
Jawz84 Mar 13, 2020
cee3ab7
add check for strongly typed assignments
Mar 13, 2020
fe68244
Merge remote-tracking branch 'refs/remotes/origin/1410_foreach_parall…
Mar 13, 2020
c4c4fde
Add todo comments
Mar 13, 2020
490d8d9
refactor FindAll predicates to static methods
Mar 13, 2020
6767a58
Refactor GetSessionName for performance.
Mar 14, 2020
c824fd7
use full type for string expression variable
Jawz84 Mar 19, 2020
ab64ebf
use full type name for foreach variable initialization
Jawz84 Mar 19, 2020
d5b77a9
refactor diagnostic message out to local variable
Jawz84 Mar 19, 2020
6c0e90a
WIP: apply review suggestions
Mar 19, 2020
fa7c065
apply review suggestions
Mar 20, 2020
378fb16
Merge remote-tracking branch 'refs/remotes/origin/1410_foreach_parall…
Mar 20, 2020
c1247c0
Fix CR/LF -> LF
Mar 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RuleDocumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
|[AvoidOverwritingBuiltInCmdlets](./AvoidOverwritingBuiltInCmdlets.md) | Warning | |
|[AvoidNullOrEmptyHelpMessageAttribute](./AvoidNullOrEmptyHelpMessageAttribute.md) | Warning | |
|[AvoidShouldContinueWithoutForce](./AvoidShouldContinueWithoutForce.md) | Warning | |
|[UseUsingScopeModifierInNewRunspaces](./UseUsingScopeModifierInNewRunspaces.md) | Warning | |
|[AvoidUsingCmdletAliases](./AvoidUsingCmdletAliases.md) | Warning | Yes |
|[AvoidUsingComputerNameHardcoded](./AvoidUsingComputerNameHardcoded.md) | Error | |
|[AvoidUsingConvertToSecureStringWithPlainText](./AvoidUsingConvertToSecureStringWithPlainText.md) | Error | |
Expand Down
65 changes: 65 additions & 0 deletions RuleDocumentation/UseUsingScopeModifierInNewRunspaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# UseUsingScopeModifierInNewRunspaces

**Severity Level: Warning**

## Description

If a ScriptBlock is intended to be run in a new RunSpace, variables inside it should use $using: scope modifier, or be initialized within the ScriptBlock.
This applies to:

- Invoke-Command *
- Workflow { InlineScript {}}
- Foreach-Object **
- Start-Job
- Start-ThreadJob
- The `Script` resource in DSC configurations, specifically for the `GetScript`, `TestScript` and `SetScript` properties

\* Only with the -ComputerName or -Session parameter.
\*\* Only with the -Parallel parameter

## How to Fix

Within the ScriptBlock, instead of just using a variable from the parent scope, you have to add the `using:` scope modifier to it.

## Example

### Wrong

```PowerShell
$var = "foo"
1..2 | ForEach-Object -Parallel { $var }
```

### Correct

```PowerShell
$var = "foo"
1..2 | ForEach-Object -Parallel { $using:var }
```

## More correct examples

```powershell
$bar = "bar"
Invoke-Command -ComputerName "foo" -ScriptBlock { $using:bar }
```

```powershell
$bar = "bar"
$s = New-PSSession -ComputerName "foo"
Invoke-Command -Session $s -ScriptBlock { $using:bar }
```

```powershell
# Remark: Workflow is supported on Windows PowerShell only
Workflow {
$foo = "foo"
InlineScript { $using:foo }
}
```

```powershell
$foo = "foo"
Start-ThreadJob -ScriptBlock { $using:foo }
Start-Job -ScriptBlock {$using:foo }
```
45 changes: 45 additions & 0 deletions Rules/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1114,12 +1114,27 @@
<value>ReviewUnusedParameter</value>
</data>
<data name="ReviewUnusedParameterDescription" xml:space="preserve">
<value>Ensure all parameters are used within the same script, scriptblock, or function where they are declared.</value>
</data>
<data name="ReviewUnusedParameterError" xml:space="preserve">
<value>The parameter '{0}' has been declared but not used. </value>
</data>
<data name="ReviewUnusedParameterName" xml:space="preserve">
<value>ReviewUnusedParameter</value>
</data>
</root>
<value>Ensure all parameters are used within the same script, scriptblock, or function where they are declared.</value>
</data>
<data name="ReviewUnusedParameterError" xml:space="preserve">
<value>The parameter '{0}' has been declared but not used. </value>
</data>
<data name="ReviewUnusedParameterName" xml:space="preserve">
<value>ReviewUnusedParameter</value>
</data>
<data name="UseUsingScopeModifierInNewRunspacesCommonName" xml:space="preserve">
<value>Use 'Using:' scope modifier in RunSpace ScriptBlocks</value>
</data>
<data name="UseUsingScopeModifierInNewRunspacesDescription" xml:space="preserve">
<value>If a ScriptBlock is intended to be run as a new RunSpace, variables inside it should use 'Using:' scope modifier, or be initialized within the ScriptBlock.</value>
</data>
<data name="UseUsingScopeModifierInNewRunspacesName" xml:space="preserve">
<value>UseUsingScopeModifierInNewRunspaces</value>
</data>
<data name="UseUsingScopeModifierInNewRunspacesError" xml:space="preserve">
<value>The variable '{0}' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier.</value>
</data>
<data name="UseUsingScopeModifierInNewRunspacesCorrectionDescription" xml:space="preserve">
<value>Replace {0} with {1}</value>
</data>
</root>
Loading