I'm using SSIS and Powershell to check if a file is locked or not.
I have the below Expression in a variable called 'Cmd':
"-NoProfile -ExecutionPolicy ByPass -Command \"try { [IO.File]::OpenWrite(‘” + @[User::TestFilePath] + “‘).close();0 } catch {999}"
Which evaluates to this:
-NoProfile -ExecutionPolicy ByPass -Command "try { [IO.File]::OpenWrite(‘” + @[User::TestFilePath] + “‘).close();0 } catch {999}
Using the Execute Process Task I then call the Cmd variable above and have Success and Failure constraints after it. The Process always reports a Success, even I open the file in question, rename or even delete it.
If I then amend to the below, the Task will always fail, even if its not open:
"-NoProfile -ExecutionPolicy ByPass -Command \"try { [IO.File]::OpenWrite(‘” + @[User::TestFilePath] + “‘).close();exit 0} catch {exit 999}"
What am I missing?