Ivan
Ivan

Reputation: 91

How to add an exception to wait for process to close command in PowerShell?

I have a simple bit of code below which runs an exe successfully 98% of the time. PowerShell waits for FineCmd.exe to close using | Out-Null.

Note: FineCmd.exe runs FineReader.exe

However, sometimes FineReader.exedisplays a simple error message box which stops the for loop. Once the user manually clicks OK and closes FineReader.exe, PowerShell continues with the for loop.

How can I get PowerShell to:

  1. realise that an error has occurred in FineReader.exe
  2. (force) close FineReader.exe
  3. repeat the loop iteration where the error occurred?

The entire code is below:

$path = "C:\OCR\process.txt"
$files = Get-Content $path
[System.Collections.ArrayList]$FPaths = $files
cd "C:\Program Files (x86)\ABBYY FineReader 12"

for ($i = $FPaths.count - 1; $i -ge 0; $i--) {
.\FineCmd.exe $FPaths[$i] /lang Mixed /out $FPaths[$i] /quit | Out-Null
$FPaths.RemoveAt($i)
$Date = Get-Date -Format g
Write-Host $i "Files to go at " $Date
$FPaths | Out-File $path
}

Upvotes: 1

Views: 348

Answers (0)

Related Questions