2017-02-21 91 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功能,请将其注释掉。

相关问题