Я могу думать о двух способов решения этой:
- трубу исполняемый вызов в Out-Null
- раскошеливаться вызов cmd.exe/с (как показано на @ BobLobLaw годов ответ)
Я сделал ваш образец кода более конкретным, чтобы я мог запускать и тестировать свои решения; надеюсь, это будет переводиться. Вот что я начал с того, чтобы быть эквивалентным вашему образцу кода, т. Е. Скрипт выполняется без ожидания завершения исполняемого файла.
# I picked a specific program
Set-Alias program "notepad.exe"
# And put some values in $all_data, specifically the paths to three text files.
$all_data = Get-Item B:\matt\Documents\*.txt
# This opens each file in notepad; three instances of notepad are running
# when the script finishes executing.
$all_data | %{ program "$_" }
Вот тот же код, что и выше, но трубопровод к Out-Null
заставляет скрипт ждать на каждой итерации цикла.
# I picked a specific program
Set-Alias program "notepad.exe"
# And put some values in $all_data, specifically the paths to three text files.
$all_data = Get-Item B:\matt\Documents\*.txt
# Piping the executable call to out-null forces the script execution to wait
# for the program to complete. So in this example, the first document opens
# in notepad, but the second won't open until the first one is closed, and so on.
$all_data | %{ program "$_" | Out-Null}
И, наконец, тот же код (более или менее) с использованием cmd /c
для вызова исполняемого файла и сделать ожидание сценария.
# Still using notepad, but I couldn't work out the correct call for
# cmd.exe using Set-Alias. We can do something similar by putting
# the program name in a plain old variable, though.
#Set-Alias program "notepad.exe"
$program = "notepad.exe"
# Put some values in $all_data, specifically the paths to three text files.
$all_data = Get-Item B:\matt\Documents\*.txt
# This forces script execution to wait until the call to $program
# completes. Again, the first document opens in notepad, but the second
# won't open until the first one is closed, and so on.
$all_data | %{ cmd /c $program "$_" }
Это не относится к вашей проблеме, но у вас есть ненужный цикл foreach в коде. Вы можете упростить цикл для одного из них: 'Еогеасп ($ данные в $ all_data) { программа/команда: данные $/closeonend:: обновление/путь 2} }' или '$ all_data | % {program/command: update/path: "$ _"/closeonend: 2} ' –
Итак,« вам нужно подождать, пока внешний вызов не завершится до следующей итерации »? – Neolisk