-
Notifications
You must be signed in to change notification settings - Fork 4
Loops
Wiki Loop - is a sequence of instructions executed repeatedly
+ Convenient construction of iterating over objects or properties one by one per iteration
+ Convenient design of repetition of code blocks, functions, etc.
- Resource-intensive functionality
- Easy to make an error with nesting and number of elements that can lead to decreased performance
- Ability to create an eternal loop (script freeze)
General commands help:
-
Foreach -Parrallel в PowerShell 5 (Workflows)
-
Get-Help about_Foreach-Parrallel
Foreach -Parrallel в PowerShell 7
Until <Statement> will not be $True <Code> will be executed in a loop
For any <Statement>, <Code> will be executed at least once.
$i = 1
Do {
$i
$i++
} Until ( $i -ge 5 )Conventional mechanism allowing to repeat a piece of code several times until <Statement> will be $True
Identical mechanisms exists in all languages.
For ( $i = 1 ; $i -le 5 ; $i++ ) { $i }For ($i = $k = 1 ; $i -le 5; write-host "k : $k") {
$i
$i++
$k = $i
}If <Statement> is $True - code will be executed
Unlike Do\Until if <Statement> will be $False code inside the loop will not be executed even once
$i = 1
While($i -le 5){
$i
$i++
}There are 2 different constructions – Foreach and Foreach-Object, where in some cases Foreach is an alias for Foreach-Object, and in some cases these constructions differ in their principle of operation. In example 1 and example 2, both the Foreach-Object and the Foreach alias can be used. Example 3 Foreach-Object cannot be used.
1..5 | Foreach-Object { $_ }ForEach-Object -InputObject @(1..5) -Process { $_ }
# Instead $_ could be used $PSItemForEach($i in @(1..10)) { Test-Connection -Quiet "172.28.110.$i" -Count 1 }All statements for PowerShell 5 are true except for Workflow, which in version 7 is designated as Deprecated At the same time, Foreach-Object -Parallel in PoSh7 works without additional wrappers (out of box)
1..10 | ForEach-Object -Parallel { Test-Connection -Quiet "172.28.110.$_" -Count 1 }Inside the loop could be used control commands for skipping or ending loop(s)
In this Example in the output will be skipped each combination where j == 7
Same way this will work with do\until, for and while
Foreach($i in @(1..5)) {
Foreach($j in @(9..5)) {
if ($j -eq 7){continue}
Write-Host "i = $i || j = $j"
}
}In this Example command will work totally different as in Example 1 Code will be ended when j == 7
1..5 | Foreach {
$i = $_
9..5 | Foreach {
$j = $_
if ($j -eq 7){continue}
Write-Host "i = $i || j = $j"
}
}Processing of the nested foreach will be interrupted if j == 7
Same way this will work with do\until, for and while
Foreach($i in @(1..5)) {
Foreach($j in @(9..5)) {
if ($j -eq 7){break}
Write-Host "i = $i || j = $j"
}
}
# Output:
i = 1 || j = 9
i = 1 || j = 8
i = 2 || j = 9
i = 2 || j = 8
i = 3 || j = 9
i = 3 || j = 8
i = 4 || j = 9
i = 4 || j = 8
i = 5 || j = 9
i = 5 || j = 8Will work similarly to the continue example (Example №2)
1..5 | Foreach {
$i = $_
9..5 | Foreach {
$j = $_
if ($j -eq 7){break}
Write-Host "i = $i || j = $j"
}
}If (j == 7) condition is met, an exit will be made to the tagged point.
Thus, several (selected) loops can be exited
Same way this will work with do\until, for and while
:quit Foreach($i in @(1..5)) {
Foreach($j in @(9..5)) {
if ($j -eq 7){break quit}
Write-Host "i = $i || j = $j"
}
}