调用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 脚本,请首先设置必要的变量。
然后添加一个执行进程任务。在“进程”选项卡上,设置“可执行文件”字段。
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 包。
并且该文件被复制到输出文件夹。
顺便说一句,将 $ 放在双引号内时需要小心,因为变量名称会被替换,如下所示: