SQL SSIS下使用WinSCP调度SFTP的服务器代理

SQL Server agent for scheduling SFTP using WinSCP under SSIS

我有一个批处理脚本,它生成一个 WinSCP 上传脚本来将文件上传到 SFTP 位置。现在,当我通过命令提示 运行 批处理文件时 - 它 运行 成功并加载了它。我通过 SSIS 执行进程任务调用了相同的任务 - 它 运行s 成功并加载了它。现在,当我把相同的东西放在 SQL 代理上时 - 我尝试了以下两个选项:

  1. 使用操作系统 (CmdExec) - cmd.exe /c "\.bat"
  2. 已将 SSIS 包添加到 SSISDB 并将其添加为作业步骤。

使用上述两个选项,作业显示成功 运行。但是文件没有上传!对正在发生的事情有什么想法吗?

这是我的批处理脚本:

echo off
SET winscp=C:\"Program Files (x86)"\WinSCP\WinSCP.com
SET stagingDirectory=\<staging path>\
SET scriptPath=\<ScriptPath>\UploadScript.txt
SET ftpHost=xx.xx.xx.xx
SET ftpUser=user
SET ftpPass=password
SET fileName=Test.xlsx
SET ftpFlags=
@REM ftpFlags: -explicit

echo deleting uploadScript if it already exists

IF EXIST %scriptPath% del /F %scriptPath%
IF EXIST %scriptPath% exit 1

echo Generating WINSCP Upload Script

>>%scriptPath% echo option batch abort
>>%scriptPath% echo option confirm off
>>%scriptPath% echo open sftp://%ftpUser%:%ftpPass%@%ftpHost% %ftpFlags%
>>%scriptPath% echo option transfer binary
>>%scriptPath% echo put %stagingDirectory%%fileName% /
>>%scriptPath% echo close
>>%scriptPath% echo exit

echo Launching WINSCP upload

start /wait %winscp% /console /script=%scriptPath%

您的变量似乎设置不正确。要在路径和变量中使用 space 进行管理,您必须将整个路径或整个变量放在引号中。

set "winscp=C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start "%winscp%"
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"

set winscp="C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start %winscp%
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"

还有一点,你必须检查这个文件:UploadScript.txt 因为你的脚本添加了新行而不是重新制作文件。

将此行更改为 >%scriptPath% echo option batch abort 而不是 >>%...

啊,我没注意IF EXIST

当您通过 start 启动 WinSCP(为什么?)时,退出代码不会传播到 SSIS。因此,如果脚本失败,您将永远学不会。它很可能会失败。

您还应该启用日志记录,以便您可以看到问题所在。

您应该使用此代码将 WinSCP 退出代码传播到 SSIS 并启用日志记录:

%winscp% /log=\<ScriptPath>\UploadScript.log /script=%scriptPath%
exit /b %ERRORLEVEL%

(注意是winscp.com does not have the /console parameter


无论如何,一个明显的问题是您没有指定 expected SSH host key in the script. When you run the script manually, you probably have the key cached in the registry of your Windows account. But under the SSIS a different account is used, and its host key cache is likely empty. You should add the -hostkey switch to the open command in the script to make the script independent on the cache. See Where do I get SSH host key fingerprint to authorize the server?

测试脚本时,将/ini=nul参数添加到isolate the script from your configuration


对于此提示和其他提示,在 SSIS 下调试 WinSCP 运行 时,请参阅 My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?

最后,请参阅 WinSCP SFTP Task for SSIS