运行带有参数的外部程序,然后等待其在Powershell中结束

时间:2018-12-25 08:50:01

标签: powershell powershell-v5.1

实际上,我找到了很多解决此问题的方法,但没有一个有效。 我想在Powershell中运行的程序是Reaper-数字音频工作站,我将使用其命令行工具对PS脚本中的音频文件进行批处理。与收割者相关的代码如下:

reaper -batchconvert $output_path\audio\Reaper_filelist.txt

我将使用带有Start-Process参数的-wait来让脚本等待其结束,然后继续执行下一个代码行,即Rename-Item函数。

ls $processed_audio_path | Rename-Item -NewName {$_.name.Replace("- ", "")}

如果PS不等待该过程完成,则下一行将引发错误,例如“在目录中找不到此类文件”。

我发现的建议是hereherehere。但是这些都不起作用。问题是收割者不接受将参数单独添加为:

$exe = "reaper"
$arguments = "-batchconvert $output_path\audio\Reaper_filelist.txt"
Start-Process -filepath $exe -argumentlist $arguments -wait

或:

Start-Process -filepath reaper -argumentlist "-batchconvert $output_path\audio\Reaper_filelist.txt" -Wait

或:

Start-Process -filepath reaper -argumentlist @("-batchconvert", "$output_path\audio\Reaper_filelist.txt") -Wait

像上面的第一行代码一样,它只能作为一个整体块正常工作。 那我现在该怎么办?

2 个答案:

答案 0 :(得分:0)

正如一个评论所提到的,可能是该进程启动了另一个进程,导致powershell在脚本中移动。 如果是这样,您可以使用while语句等待文件创建。

while (!(Test-Path "$output_path\audio\Reaper_filelist.txt")) { Start-Sleep 10 }

答案 1 :(得分:0)

我找到了解决此问题的方法。
我认为我需要描述更多有关此的上下文。我总是在Windows后台启动Reaper,当脚本调用Reaper的BatchConvert函数时,它将启动Reaper的另一个实例,因此在转换音频文件时我得到了2个实例。这-Reaper的实例-可能是限制以下代码的可靠条件。我从herehere中发现了一些有用的东西。

最后,我得到了这样的代码,它可以正常工作:

# Batch converting through Reaper FX Chain
reaper -batchconvert $output_path\audio\Reaper_filelist.txt
while (@(Get-Process reaper).Count -eq 2){
    Start-Sleep -Milliseconds 500
    }
# Correct the Wrong file name produced by Reaper
ls $processed_audio_path | Rename-Item -NewName {$_.name.Replace("- ", "")}