2016-08-02 604 views
11

我們被要求設置從我們的一臺服務器到SFTP站點的自動上傳。每個星期一早上將有一個文件從數據庫導出到文件管理器,他們希望文件在星期二上傳到SFTP。我們正在使用的當前身份驗證方法是用戶名和密碼(我相信有一個選項也有密鑰文件,但用戶名/密碼選項被選中)。使用PowerShell將文件上傳到SFTP

我設想的方式是讓一個腳本坐在服務器上,由Windows任務計劃程序在特定時間(星期二)運行,以便抓取相關文件將其上傳到SFTP,然後將其移至其他位置以備用途。

例如:

  • 本地目錄:C:\FileDump

  • SFTP目錄:/Outbox/

  • 備份目錄:C:\Backup

我試過幾件事情在這個點WinSCP是其中之一,以及SFTP PowerShell Snap-In,但到目前爲止沒有爲我工作。

這將在Windows Server 2012R2上運行。
當我運行Get-Host我的控制檯主機版本是4.0。

謝謝。

+0

我有一個FTP腳本使用System.Net.WebRequestMethods + FTP 我用 http://www.thomasmaurer.ch/2010/11/powershell-ftp-upload-and-download/ 作爲基地。如果您需要更多幫助,我可以清理我的代碼以供公開發布。 –

+0

@GlenBuktenica問題是關於SFTP,而不是FTP。 –

回答

13

當前沒有用於執行SFTP部分的內置PowerShell方法。您必須使用像psftp.exe或PowerShell模塊(如Posh-SSH)。

這裏使用一個例子Posh-SSH

# Set the credentials 
$Password = ConvertTo-SecureString 'Password1' -AsPlainText -Force 
$Credential = New-Object System.Management.Automation.PSCredential ('root', $Password) 

# Set local file path, SFTP path, and the backup location path which I assume is an SMB path 
$FilePath = "C:\FileDump\test.txt" 
$SftpPath = '/Outbox' 
$SmbPath = '\\filer01\Backup' 

# Set the IP of the SFTP server 
$SftpIp = '10.209.26.105' 

# Load the Posh-SSH module 
Import-Module C:\Temp\Posh-SSH 

# Establish the SFTP connection 
New-SFTPSession -ComputerName $SftpIp -Credential $Credential 

# Upload the file to the SFTP path 
Set-SFTPFile -SessionId 0 -LocalFile $FilePath -RemotePath $SftpPath 

# Disconnect SFTP session 
(Get-SFTPSession -SessionId 0).Disconnect() 

# Copy the file to the SMB location 
Copy-Item -Path $FilePath -Destination $SmbPath 

一些其他注意事項:

  • 你必須下載辣妹-SSH模塊,可以安裝到你的用戶模塊目錄(如C:\ Users \ jon_dechiro \ Documents \ WindowsPowerShell \ Modules),只需使用該名稱進行加載或將其放在任何位置並像我在上面的代碼中那樣加載它。
  • 如果在腳本中擁有憑據是不可接受的,則必須使用憑證文件。如果您需要幫助,我可以更新一些細節或指向一些鏈接。
  • 根據需要更改路徑,IP等。

這應該給你一個體面的起點。

19

你沒告訴我們你有什麼特別的問題與WinSCP賦予,所以我真的只重複什麼是WinSCP賦予文檔。

  • Download WinSCP .NET assembly
    截至目前最新的包裝是WinSCP-5.13-Automation.zip;
  • Extract.zip沿您的腳本存檔;
  • 使用這樣的代碼(根據官方公佈的PowerShell upload example):

    # Load WinSCP .NET assembly 
    Add-Type -Path "WinSCPnet.dll" 
    
    # Setup session options 
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 
        Protocol = [WinSCP.Protocol]::Sftp 
        HostName = "example.com" 
        UserName = "user" 
        Password = "mypassword" 
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" 
    } 
    
    $session = New-Object WinSCP.Session 
    
    try 
    { 
        # Connect 
        $session.Open($sessionOptions) 
    
        # Upload 
        $session.PutFiles("C:\FileDump\export.txt", "/Outbox/").Check() 
    } 
    finally 
    { 
        # Disconnect, clean up 
        $session.Dispose() 
    } 
    

你可以有WinSCP賦予生成上傳你的PowerShell腳本:

  • 用WinSCP GUI登錄到你的服務器;
  • 導航到遠程文件面板中的目標目錄;
  • 在本地文件面板中選擇要上傳的文件;
  • 調用上傳命令;
  • Transfer options dialog,去傳輸設置>生成代碼;
  • 在Generate transfer code對話框中,選擇.NET assembly code tab;
  • 選擇PowerShell的語言。

你會得到一個類似的代碼上面填寫所有的會話和傳送設置。

Generate transfer code dialog

(我的WinSCP的作者)

+1

昨天我試圖找出這些東西的問題。然後我找到了你的帖子。這有助於很多! 我在短短几分鐘內就完成了我的測試工作,而且這一切似乎都能奏效。那是一個超級漂亮的功能,先生。 有時你需要的只是一個很好的例子。 –

+0

哇,很好的信息,我沒有意識到WinSCP有這個功能!我愛你的軟件:)一種嘮叨的問題,如果WinSCP可以允許更多的連接過程自定義,那將是美好的。我可以使用PLINK將SCP文件發送到我的思科設備,因爲直接SCP只需要進行一次認證,但如果不使用只需要密碼作爲響應的「En」就無法連接並獲得DIR,而且我不能弄清楚如何設置WinSCP來做到這一點。也許我們有一天可以更好地控制使用XMLS的連接過程? –

+0

WinSCP就像我的sftp問題的冠軍一樣工作 – digitalslavery