SSIS 和 Powershell 文件锁 [英] SSIS and Powershell File Lock
问题描述
我正在使用 SSIS 和 Powershell 检查文件是否被锁定.
I'm using SSIS and Powershell to check if a file is locked or not.
我在名为Cmd"的变量中有以下表达式:
I have the below Expression in a variable called 'Cmd':
"-NoProfile -ExecutionPolicy ByPass -Command \"try { [IO.File]::OpenWrite(‘" + @[User::TestFilePath] + "‘).close();0 } catch {999}"
评估结果如下:
-NoProfile -ExecutionPolicy ByPass -Command "try { [IO.File]::OpenWrite(‘" + @[User::TestFilePath] + "‘).close();0 } catch {999}
使用 Execute Process Task 我然后调用上面的 Cmd 变量并在它之后有成功和失败约束.进程总是报告成功,即使我打开有问题的文件,重命名甚至删除它.
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}"
我错过了什么?
推荐答案
如果它对任何人有帮助,我找到了一个解决方案.我没有通过表达式调用 PS 脚本,而是通过进程任务调用实际的 PS 文件,在 PS 文件中包含以下内容:
If it helps anyone, I found a resolution. Instead of calling the PS script via an expression, I just called the actual PS file via a Process Task with the below in in the PS file:
$file = "\\xxxx\xxxxx\xxxxxxxx\test.log"
try { [IO.File]::OpenWrite($file).close();exit 0 } catch { exit 999}
这篇关于SSIS 和 Powershell 文件锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!