简体   繁体   中英

Start-Process -Wait Not Working

I have powershell script that calls another .exe file. Start-Process ".\\file.exe" -wait

file.exe is a simple windows with some message and has one only button to close the the .exe.

This is the first line in the main script runs. I want to wait and let the user to click close to proceed with the rest of the code.

However, the process starts, but the main program does not wait and starts executing the rest of the code.

Is there something else I need to do?

This recommendation is possibly not the best approach, but came to mind with starting a process. The trick is letting the script know that the other process is running, and it must be communicated that the other process has finished. One method is to have the EXE communicate by writing to a file right before it closes, the script is constantly reading the file waiting for a certain output entered by the EXE. This is dirty though, so another way is to use Windows Forms in Powershell and call the form in PowerShell instead of an EXE (I can elaborate if needed). My third solution is probably the easiest to implement, but not the cleanest. Before launching the EXE, get all processes and store them in an array. Then I would launch the EXE and get processes again, but this time looking for the one that is not in that array. This will likely be your EXE, which you can have the script then look for in a while loop and wait for that process to drop. Example:

$processes = (Get-Process).Id
Start-Process ".\file.exe"
Start-Sleep -Seconds 1
$exeProc   = (Get-Process).Id | ?{$processes -notcontains $_}
$running   = (Get-Process | ?{$_.Id -match $exeProc}).Id
while($running){
    Start-Sleep -Seconds 1
    $running = (Get-Process | ?{$_.Id -match $exeProc}).Id
}

not sure if this is what you're looking for, but you can use NSI wrapper to run the first script using ExecWait "powershell -File .\\file.exe" and that should do the job.

run any other script after that and it will do what you're looking for.

Start-Process seems to create a new window for the process, even though you specify the -Wait switch.

What has worked for me before is adding a -NoNewWindow switch to Start-Process

Try

Start-Process ".\file.exe" -NoNewWindow -Wait

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM