Jeremy Reed
Jeremy Reed

Reputation: 319

How to make powershell wait for exe to install?

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.

Upvotes: 6

Views: 9622

Answers (1)

Paul Rowland
Paul Rowland

Reputation: 8352

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

Upvotes: 9

Related Questions