Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: IOException try/catch isn't working

Tags:

powershell

I have a PS script that kicks off every 5 minutes to check for newly dropped folders and move them. Problem is that sometimes items within the folder are still being written to, in which case the script errors with:

Move-Item : The process cannot access the file because it is being used by another process. [Move-Item], IOException + FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

I've tried the following try/catch block but it still errors on the same "Move-Item" line. Any thoughts to what I'm doing wrong here?

          try {
           Move-Item -Force "$fileString" $fileStringFixed
          }
          catch [System.IO.IOException] {
           return
          }

Thank you.

like image 313
orbitron Avatar asked Jun 22 '10 23:06

orbitron


People also ask

How do you handle try catch in PowerShell?

The catch keyword is followed by an optional list of error type specifications and a statement list. If a terminating error occurs in the try block, PowerShell searches for an appropriate catch block. If one is found, the statements in the catch block are executed. The catch block can specify one or more error types.

Can you nest try catch PowerShell?

A try-catch-finally statement can be nested beneath another. This is most appropriate when a different approach is required by a smaller section of code. A script perform setup actions, then working on a number of objects in a loop is a good example of one that might require more than one try-catch statement.

How do you catch non-terminating errors in PowerShell?

The above error is generated by cmdlet and it is a non-terminating error. You can handle both the terminating and non-terminating error (by converting them into terminating error) using ErrorAction cmdlet, $ErrorActionPreference variable and try, catch, and finally blocks.


2 Answers

Try/catch statements can only catch terminating errors (these usually indicate a severe error). PowerShell also has the concept of non-terminating errors. The file-in-use error you see is a non-terminating error. This is good from the perspective that if you were moving thousands of files and one had its target in use, the command doesn't crap out it keeps going. You have two choices here. You can ignore these errors by setting the ErrorAction parameter to SilentlyContinue (value of 0) e.g.:

Move-Item foo bar -ErrorAction SilentlyContinue

Or you can convert the non-terminating error to a terminating error by setting this same parameter to 'Stop' and then use the try/catch although don't filter by IOException because PowerShell wraps the exception e.g.:

try { Move-Item .\About_This_Site.txt vmmap.exe -ErrorAction Stop } `
catch {$_.GetType().FullName}
System.Management.Automation.ErrorRecord
like image 92
Keith Hill Avatar answered Sep 22 '22 13:09

Keith Hill


I was able to solve this by adding -ErrorAction Stop to the Move-Item command. That seems to force it to throw an error as intended, instead of doing whatever it wants.

like image 32
Prank Monkey Avatar answered Sep 26 '22 13:09

Prank Monkey