2013-02-06 90 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一直都不是我的朋友。 –