2017-02-21 161 views
0

我正在使用的供應商將zip文件上傳到FTP。我需要下載任何上傳和處理,根據需要。使用Powershell下載FTP文件夾中的所有文件

使用Powershell,如何從FTP文件夾下載*.*

(在參考https://social.technet.microsoft.com/Forums/office/en-US/744ee28a-9340-446a-b698-4b96e081b501/download-files-from-ftp-server?forum=winserverpowershell

# Config 
$Username = "user" 
$Password = "password" 
$LocalFile = "C:\tools\file.zip" 
$RemoteFile = "ftp://myftpserver:22/Folder1/Folder/file.csv" 

# Create a FTPWebRequest 
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) 
$FTPRequest.Credentials = New-Object  System.Net.NetworkCredential($Username,$Password) 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
$FTPRequest.UseBinary = $true 
$FTPRequest.KeepAlive = $false 
$ftpRequest.EnableSsl = $true 
# Send the ftp request 
$FTPResponse = $FTPRequest.GetResponse() 
# Get a download stream from the server response 
$ResponseStream = $FTPResponse.GetResponseStream() 
# Create the target file on the local system and the download buffer 
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
[byte[]]$ReadBuffer = New-Object byte[] 1024 
# Loop through the download 
    do { 
     $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
     $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
    } 
    while ($ReadLength -ne 0) 

有沒有什麼辦法讓$參數】remotefile像ftp://myftpserver:22/Folder1/Folder/*.zipftp://myftpserver:22/Folder1/Folder/*.*

我的道歉,如果有一個職位相同。我看到一些相似的,但不夠接近回答這個問題。

+0

[Powershell FTP下載文件和子文件夾]的可能重複(http://stackoverflow.com/questions/37080506/powershell-ftp-download-files-and-subfolders) –

回答

0

我已經創建了PowerShell SFTP,FTP和FTPS腳本。你將不得不從我的github下載它。因爲我不信任他們,所以我不使用任何第三方應用程序。我使用的是用於SFTP部分的REBEX.dll和用於FTP和FTPS的.Net WebRequest。

https://github.com/FallenHoot/ZOTECH-PS/blob/master/SFTP.zip

如果你有問題的理解代碼,請讓我知道。如果您不打算使用Get-SFTP功能,請將其註釋掉。

相關問題