Handling an ExifTool timeout for ReadLine in a PowerShell script

Started by SailorGuy, April 09, 2022, 07:18:06 PM

Previous topic - Next topic

SailorGuy

I have a PowerShell script that parses various date fields from image files.  The script loops through a list of files and invokes an "in memory" instance of ExifTool with various date tags. I use StdIn and StdOut redirection (WriteLine and ReadLine) to issue Exiftool commands.

Usually the script works fine and processes the ExifTool commands with no problems.  However, every now and then, ExifTool hangs when parsing a tag via ReadLine and my script has to be manually killed. My goal is to set a 5 second timer that will abort the ReadLine if no response is received and present some timeout handling logic.

I am not an expert in PowerShell scripting, but I found code samples that use a Try/Catch/Finally method to set a timeout value so if the ReadLine doesn't respond in time, a error is thrown and I can process the error. 

I have tried coding the Try/Catch/Finally logic but with no success.

I have attached excerpts from my PowerShell script that show all the ExifTool-related code segments.  The ExifToolReadTag function is where I am having my problems.

Am I on the right track with using the $p.ReadTimeout value and Try/Catch/Finally?

Any help or guidance is greatly appreciated.

Thank you,

Sailor Guy