2
我想通過使用WinSCP的SFTP從遠程目錄下載一些文件。使用WinSCP通過SFTP下載文件的VBScript失敗,無法使用密鑰文件(無法打開文件)
直到現在我有這個VBScript。請注意,通過私鑰(.ppk文件)建立到SFTP服務器的連接。
的代碼是這樣如下:
Function SFTPDownload(byVal sLocalPath, byVal sRemotePath, byVal sRemoteFile)
Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")
sRemotePath = Trim(sRemotePath)
sLocalPath = Trim(sLocalPath)
sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory
oFTPScriptShell.CurrentDirectory = sLocalPath
sFTPScript = sFTPScript & "option batch on" & vbCRLF
sFTPScript = sFTPScript & "option confirm off"& vbCrLf
sFTPScript = sFTPScript & "option transfer binary" & vbCrLf
sFTPScript = sFTPScript & "open username:@sftp.server.com:22 -privatekey=mypriviatekey.ppk" & vbCrLf
sFTPScript = sFTPScript & "cd " & sRemotePath & vbCrLf
sFTPScript = sFTPScript & "get " & sRemoteFile & vbCRLF
sFTPScript = sFTPScript & "close" & vbCrLf
sFTPScript = sFTPScript & "exit" & vbCrLf
sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
'Write the input file for the sftp command to a temporary file.
Set oFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
oFTPScript.WriteLine(sFTPScript)
oFTPScript.Close
Set oFTPScript = Nothing
sCmd = """C:\Program Files (x86)\WinSCP\WinSCP.com"" -script=" & sFTPTempFile
oFTPScriptShell.run sCmd
Wscript.Sleep 1000
' Get rid of temp file used for input to sftp
oFTPScriptFSO.DeleteFile(sFTPTempFile)
Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function
Dim myResult
myResult = SFTPDownload("F:\WLMS_TEAM\TOUHID\TT Files\", "/done", "EE*.csv")
腳本打開的WinSCP命令提示符,然後給了我這樣的:
無法使用密鑰文件(無法打開文件)
任何想法如何使它工作?