Skip to content

Commit 7182edb

Browse files
Jos KoelewijnJos Koelewijn
Jos Koelewijn
authored and
Jos Koelewijn
committed
Add documentation
1 parent 0ab5fbf commit 7182edb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# AvoidUnInitializedVarsInNewRunspaces
2+
3+
**Severity Level: Warning**
4+
5+
## Description
6+
7+
If a scriptblock is intended to be run as a new runspace, variables inside it should use $using: directive, or be initialized within the scriptblock.
8+
9+
## How to Fix
10+
11+
Within `Foreach-Object -Parallel {}`, instead of just using a variable from the parent scope, you have to use the `using:` directive:
12+
13+
## Example
14+
15+
### Wrong
16+
17+
``````PowerShell
18+
$var = "foo"
19+
1..2 | ForEach-Object -Parallel { $var }
20+
``````
21+
22+
### Correct
23+
24+
``````PowerShell
25+
$var = "foo"
26+
1..2 | ForEach-Object -Parallel { $using:var }
27+
``````

0 commit comments

Comments
 (0)