Skip to content

Instantly share code, notes, and snippets.

@jahands
Created April 17, 2020 19:42
Show Gist options
  • Save jahands/be721ac325b6ac410a34ddc8a932f06b to your computer and use it in GitHub Desktop.
Save jahands/be721ac325b6ac410a34ddc8a932f06b to your computer and use it in GitHub Desktop.
Run things in parallel in PowerShell 7
Function Foo {
@(1..2) | ForEach-Object -Parallel {
switch ($_) {
1 {
Return 1
}
2 {
# This could be something that takes a while
# like a network call, etc
Start-Sleep 3
Write-Warning 'Finished Foo'
}
}
}
}
Function Bar {
Foo | ForEach-Object {
$a = $_
@(1..2) | ForEach-Object -Parallel {
switch ($_) {
1 {
$r = "$($_), 2"
Return $r
}
2 {
Start-Sleep 3
Write-Warning 'Finished Bar'
}
}
}
}
}
Bar | ForEach-Object {
# This way we get to use the value
# asap while all the defered methods run
$x = $_
# Use $x
Write-Host $x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment