【问题标题】:Passing variables to a powershell script within a SSIS package将变量传递给 SSIS 包中的 powershell 脚本
【发布时间】:2019-04-03 22:35:16
【问题描述】:

我正在尝试将 SSIS 变量传递到通过 SSIS 中的流程任务运行的 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

如果我硬编码参数,我可以让 PowerShell 脚本运行良好,但是一旦我将它更改为变量,变量值就不会通过

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

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

参数字符串正在正确构建,所以我知道变量值正在传入

-ExecutionPolicy Unrestricted -File "C:\Qarefresh.ps1" "DbaseA.mdf"

这是表达式的代码

"-ExecutionPolicy Unrestricted -File \"" +  "C:\\Qarefresh.ps1\" \"" + @[User::QA_FileName] + "\""

我对 PowerShell 还比较陌生,所以如果我错过了一些基本的东西,我深表歉意,但我已经快用这个来拔掉我的头发了

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

【问题讨论】:

  • 顺便说一句,您要小心将 $ 放在双引号内,因为变量名会被替换。我将更新我的答案以显示可能发生的情况。

标签: powershell ssis


【解决方案1】:

调用 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;

然后你可以像这样在命令提示符下测试脚本,传递参数。

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

文件被复制到输出文件夹。

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

然后添加一个执行流程任务。在 Process 选项卡上,设置 Executable 字段。

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

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

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

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

执行 SSIS 包。

并且文件被复制到输出文件夹中。

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

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多