首页 > 解决方案 > SSIS 和 Powershell 文件锁

问题描述

我正在使用 SSIS 和 Powershell 来检查文件是否被锁定。

我在一个名为“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 变量并在其后有成功和失败约束。进程总是报告成功,即使我打开有问题的文件,重命名甚至删除它。

如果我随后修改以下内容,任务将始终失败,即使它没有打开:

"-NoProfile -ExecutionPolicy ByPass -Command \"try { [IO.File]::OpenWrite(‘” + @[User::TestFilePath] + “‘).close();exit 0} catch {exit 999}"

我错过了什么?

标签: powershellssis

解决方案


如果它对任何人有帮助,我找到了解决方案。我没有通过表达式调用 PS 脚本,而是通过 Process Task 在 PS 文件中调用了实际的 PS 文件:

$file = "\\xxxx\xxxxx\xxxxxxxx\test.log"
try { [IO.File]::OpenWrite($file).close();exit 0 } catch { exit 999}

推荐阅读