我创建了一个测试的可执行文件,做了以下
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命令
我会怀疑,安装程序产生另一个过程做安装。 –