Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make powershell wait for an install to finish?

I have a list of Windows packages that I'm installing via powershell using the following command:

& mypatch.exe /passive /norestart

mypatch.exe is being passed from a list and it doesn't wait for the prior install to finish - it just keeps going. It builds up a huge window of installs that are pending installation. Also, I can't use $LASTEXITCODE to determine if the install succeeded or failed.

Is there anyway to make the installs wait before starting the next?

like image 993
hax0r_n_code Avatar asked Oct 16 '13 18:10

hax0r_n_code


People also ask

How do you wait for a process to finish 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 add a pause to a PowerShell script?

Adding a pause into your PowerShell Script is really simple. You can use the Start-Sleep cmdlet with: The -s parameter to specify time in seconds. The -m parameter to specify time in milliseconds.


1 Answers

JesnG is correct in using start-process, however as the question showed passing arguments, the line should be:

Start-Process "mypatch.exe" -argumentlist "/passive /norestart" -wait

The OP also mentioned determining if the install succeeded or failed. I find that using a "try, catch throw" to pick up on error states works well in this scenario

try {
    Start-Process "mypatch.exe" -argumentlist "/passive /norestart" -wait
} catch {
    # Catch will pick up any non zero error code returned
    # You can do anything you like in this block to deal with the error, examples below:
    # $_ returns the error details
    # This will just write the error
    Write-Host "mypatch.exe returned the following error $_"
    # If you want to pass the error upwards as a system error and abort your powershell script or function
    Throw "Aborted mypatch.exe returned $_"
}
like image 110
Marcus Adams Avatar answered Nov 15 '22 16:11

Marcus Adams