如何使PowerShell等待exe安装?

时间:2013-02-06 03:50:20

标签: powershell

所以我读过与这个问题相关的每个答案,但似乎没有一个答案正在发挥作用。

我已在剧本中继续这些行:

$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。

1 个答案:

答案 0 :(得分:8)

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

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

然后编写了这个powershell脚本,按照预期等待exe完成运行,然后输出文本“ps1结束”和时间

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

我想您可能会将Start-Process powershell命令与.NET框架Process对象混淆