1

我正在尝试使用 powershell 调用 perl 脚本,如下所示:

$plScript = Join-path $pwd 'Tools\myScript.pl'
$plOut = New-Item out.txt -type file -force
$plArgs = @($plScript, '--files','*.c', 'AddInc=\Tools','Addlib=\Tools')
Start-Process perl.exe -ArgumentList $plArgs -RedirectStandardOutput $plOut - wait

这失败并显示以下消息:

Start-Process : Process with an Id of 80096 is not running.
+     Start-Process <<<<  perl.exe -ArgumentList  $plArgs  -RedirectStandardOutput $plOut -wait
+ CategoryInfo          : NotSpecified: (:) [Start-Process], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.StartProcessCommand

使用 Trace-Command,我看到了失败的地方,但不明白出了什么问题

DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : System.ArgumentException: Process with an Id of 150588 is not running.
at System.Diagnostics.Process.GetProcessById(Int32 processId, String machineName)
at Microsoft.PowerShell.Commands.StartProcessCommand.start(ProcessStartInfo startInfo)
at Microsoft.PowerShell.Commands.StartProcessCommand.BeginProcessing()
at System.Management.Automation.Cmdlet.DoBeginProcessing()
at System.Management.Automation.CommandProcessorBase.DoBegin()

我错过了什么?

在 powershell v3 中运行它成功,没有错误......但我必须支持 v2 来完成这个任务......

4

1 回答 1

2

似乎 perl 进程在命令可以获取进程的 id 之前退出。试试这种方式,看看你是否能克服这个问题——也许弄清楚为什么 perl 会提前退出:

$p = Start-Process perl.exe -ArgumentList $plArgs -RedirectStandardOutput $plOut -PassThru
if (!$p.HasExited) {
    $p.WaitForExit()
} 
else {
    "Hmm why did the process exit so early, exit code was: $($p.ExitCode)"   
}
于 2013-10-27T20:20:41.953 回答