Das mag wie eine sehr sehr dumme Frage erscheinen, aber ich kann es nicht wirklich herausfinden. Ich versuche, die Funktion zu beenden, wenn sie ihren ersten Treffer (Übereinstimmung) findet und dann mit dem Rest des Skripts fortfährt.Verlassen Sie eine PowerShell-Funktion, aber setzen Sie das Skript fort
Code:
Function Get-Foo {
[CmdLetBinding()]
Param()
1..6 | ForEach-Object {
Write-Verbose $_
if ($_ -eq 3) {
Write-Output 'We found it'
# break : Stops the execution of the function but doesn't execute the rest of the script
# exit : Same as break
# continue : Same as break
# return : Executes the complete loop and the rest of the script
}
elseif ($_ -eq 5) {
Write-Output 'We found it'
}
}
}
Get-Foo -Verbose
Write-Output 'The script continues here'
Wunschergebnis:
VERBOSE: 1
VERBOSE: 2
VERBOSE: 3
We found it
The script continues here
Ich habe versucht, mit break
, exit
, continue
und return
aber keiner von ihnen erhalten mir das gewünschte Ergebnis. Danke für Ihre Hilfe.
Perfekt! Dies ist das erste und einzige Beispiel, das funktioniert! Danke Andrey :) – DarkLite1
Dies scheint gut zu 'foreach ($ b in (1..6)) {Pause}' – DarkLite1