PowerShell v2:Id未运行的进程

时间:2013-10-27 09:59:34

标签: powershell powershell-v2.0

我正在尝试使用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进行此分配...

1 个答案:

答案 0 :(得分: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)"   
}