2013-02-06 47 views
5

所以我已经阅读了与这个问题有关的每一个答案,但他们似乎都没有工作。如何让powershell等待exe安装?

我有这些线路中的脚本回事:

$exe = ".\wls1033_oepe111150_win32.exe" 
$AllArgs = @('-mode=silent', '-silent_xml=silent_install.xml', '-log=wls_install.log"') 
$check = Start-Process $exe $AllArgs -Wait -Verb runAs 
$check.WaitForExit() 

在此之后运行我有替换某些特定字符串的文件安装正则表达式检查,但无论怎样我尝试去做在程序安装时继续运行正则表达式检查。

我该怎么做才能让下一行在执行完exe后才能执行?我也尝试用管道输出Out-Null,但没有运气。

+0

我会怀疑,安装程序产生另一个过程做安装。 –

回答

8

我创建了一个测试的可执行文件,做了以下

Console.WriteLine("In Exe start" + System.DateTime.Now); 
    System.Threading.Thread.Sleep(5000); 
    Console.WriteLine("In Exe end" + System.DateTime.Now); 

然后写了预期等待exe文件来完成输出文本“PS1的终点”和时间之前运行此PowerShell脚本

push-location "C:\SRC\PowerShell-Wait-For-Exe\bin\Debug"; 
$exe = "PowerShell-Wait-For-Exe.exe" 
$proc = (Start-Process $exe -PassThru) 
$proc | Wait-Process 

Write-Host "end of ps1" + (Get-Date).DateTime 

这个下面的powershell也正确的等待exe完成。

$check = Start-Process $exe $AllArgs -Wait -Verb runas 
Write-Host "end of ps1" + (Get-Date).DateTime 

添加WaitForExit调用给我这个错误。

You cannot call a method on a null-valued expression. 
At line:2 char:1 
+ $check.WaitForExit() 
+ ~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

然而,这确实工作

$p = New-Object System.Diagnostics.Process 
$pinfo = New-Object System.Diagnostics.ProcessStartInfo("C:\PowerShell-Wait-For-Exe\bin\Debug\PowerShell-Wait-For-Exe.exe",""); 
$p.StartInfo = $pinfo; 
$p.Start(); 
$p.WaitForExit(); 
Write-Host "end of ps1" + (Get-Date).DateTime 

我想,也许你是混淆了.NET框架Process对象开始处理PowerShell命令

+0

所以看起来最后一个是赢家。前两个似乎没有工作。但是,我将两者混为一谈。谢谢你的澄清。我想Google一直都不是我的朋友。 –