且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将变量传递给 SSIS 包中的 powershell 脚本

更新时间:2023-02-07 13:23:35

调用PowerShell时需要指定参数名称.

You need to specify the parameter names when you invoke PowerShell.

这是 PowerShell 脚本.

This is the PowerShell script.

param ([string]$SourceServer, [string]$DestinationServer, [string]$Filename)

[string]$SourcePath = "\I$\***\Xp\XpSsisPowerShell\Input\";
[string]$DestinationPath = "\I$\***\Xp\XpSsisPowerShell\Output\";

[string]$Source = "\\" + $SourceServer + $SourcePath + $Filename;
[string]$Destination = "\\" + $DestinationServer + $DestinationPath + $Filename;

Copy-Item -Path $Source -Destination $Destination;

然后您可以像这样从命令提示符测试脚本,传递参数.

Then you can test the script from a command prompt like this, passing parameters.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -File I:\***\Xp\XpSsisPowerShell\Script\CopyFile.ps1 -SourceServer Baobab -DestinationServer Baobab -Filename TestData.txt

然后文件被复制到输出文件夹.

And the file gets copied to the output folder.

要从 SSIS 包调用 PowerShell 脚本,首先要设置必要的变量.

To invoke the PowerShell script from an SSIS package, first set the necessary variables.

然后添加一个执行流程任务.在进程选项卡上,设置可执行字段.

Then add an Execute Process Task. On the Process tab, set the Executable field.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

使用以下表达式设置 Arguments 字段:

Set the Arguments field with the following expression:

"-ExecutionPolicy Unrestricted -File I:\\***\\Xp\\XpSsisPowerShell\\Script\\CopyFile.ps1 -SourceServer " + @[User::SourceServer] + " -DestinationServer " + @[User::DestinationServer] + " -Filename " +  @[User::Filename]

在运行包之前确保输出文件夹为空.(这里没有作弊!)

Make sure the output folder is empty before running the package. (No cheating here!)

执行 SSIS 包.

并将文件复制到输出文件夹.

And the file is copied to the output folder.

顺便说一句,将 $ 放在双引号中时需要小心,因为变量名会被替换,如下所示:

Incidentally, you need to be careful when putting $ within double quotes, because variable names get substituted, like this: