Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make powershell wait for exe to install?

Tags:

powershell

So i've read every single answer related to this question but none of them seem to be working.

I've got these lines going on in the script:

$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()

After this runs I have a regex check on the installed files that replaces some specific strings, but no matter what I try to do it continues to run the regex check while the program is installing.

How can I make it so that the next line doesn't execute until it finishes installing the exe? I've also tried piping to Out-Null with no luck.

like image 227
Jeremy Reed Avatar asked Feb 06 '13 03:02

Jeremy Reed


People also ask

Is there a Wait command in PowerShell?

The Wait-Process cmdlet waits for one or more running processes to be stopped before accepting input. In the PowerShell console, this cmdlet suppresses the command prompt until the processes are stopped. You can specify a process by process name or process ID (PID), or pipe a process object to Wait-Process .

How do I create a Wait in PowerShell?

In PowerShell, we can use the Start-Sleep cmdlet to suspend/pause/sleep/wait the activity in a script or session for the specified period of time. You can use it for many tasks, such as waiting for an operation to be completed or pausing before repeating an operation.

Does start-process Wait?

When using the Wait parameter, Start-Process waits for the process tree (the process and all its descendants) to exit before returning control. This is different than the behavior of the Wait-Process cmdlet, which only waits for the specified processes to exit.


1 Answers

I created a test executable that did the following

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

Then wrote this powershell script which as expected waits for the exe to finish running before outputting the text "end of ps1" and the time

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

This following powershell also correctly waits for the exe to finish.

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

Adding the WaitForExit call gives me this error.

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

However this does work

$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

I think maybe you are confusing the Start-Process powershell command with the .NET framework Process object

like image 168
Paul Rowland Avatar answered Oct 20 '22 17:10

Paul Rowland