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

M.A*_*yle 6 powershell ssis

我正在尝试将SSIS变量传递到通过SSIS中的Process Task运行的PowerShell脚本,即使使用SSIS 2008,如果这有任何区别

这是使用PowerShell脚本的副本,当使用硬编码值执行时,它运行正常

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

$SourceServer = "SERVERA"
$DestinationServer = "SERVERB"
$Filename = "DbNAME.mdf"

$SourcePath = "\D$\Data\"
$DestinationPath = "\D$\Data\Backups\"

$Source = '\\' + $SourceServer + $SourcePath + $Filename
$Destination = '\\' + $DestinationServer + $DestinationPath + $Filename

copy-item -path $Source -destination $Destination -verbose
Run Code Online (Sandbox Code Playgroud)

如果我对param进行硬编码,我可以让PowerShell脚本正常运行,但是只要我将其更改为变量,变量值就不会被传递

在流程任务中,这是可执行文件

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Run Code Online (Sandbox Code Playgroud)

正确构建参数字符串,因此我知道传入的变量值

-ExecutionPolicy Unrestricted -File "C:\Qarefresh.ps1" "DbaseA.mdf"
Run Code Online (Sandbox Code Playgroud)

这是表达式的代码

"-ExecutionPolicy Unrestricted -File \"" +  "C:\\Qarefresh.ps1\" \"" + @[User::QA_FileName] + "\""
Run Code Online (Sandbox Code Playgroud)

我对PowerShell比较新,所以如果我错过了一些基本的东西,我会道歉,但我很接近用这个来拉我的头发

提前感谢您提供的任何帮助

Ric*_*dCL 9

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

这是PowerShell脚本.

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

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

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

Copy-Item -Path $Source -Destination $Destination;
Run Code Online (Sandbox Code Playgroud)

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

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -File I:\StackOverflow\Xp\XpSsisPowerShell\Script\CopyFile.ps1 -SourceServer Baobab -DestinationServer Baobab -Filename TestData.txt
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

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

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

"-ExecutionPolicy Unrestricted -File I:\\StackOverflow\\Xp\\XpSsisPowerShell\\Script\\CopyFile.ps1 -SourceServer " + @[User::SourceServer] + " -DestinationServer " + @[User::DestinationServer] + " -Filename " +  @[User::Filename]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

在此输入图像描述

执行SSIS包.

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述