2015-06-23 144 views
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命令提示符,然後給了我這樣的:

無法使用密鑰文件(無法打開文件)

任何想法如何使它工作?

回答

4

我找到了解決方案。代碼沒有問題。

我剛修改過的腳本文件,包括像下面的私鑰的完整路徑:

option batch on 
option confirm off 
option transfer binary 
open btwsale:@sftp.server.com:22 -privatekey="C:\Program Files (x86)\WinSCP\mypriviatekey.ppk" 
cd /done 
lcd "C:\Program Files (x86)\WinSCP\" 
get EE*.csv 
exit