【问题标题】:Download all files in FTP folder with Powershell [duplicate]使用Powershell下载FTP文件夹中的所有文件[重复]
【发布时间】:2017-02-21 19:22:19
【问题描述】:

与我合作的供应商将 zip 文件上传到 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/*.*

如果有相同的帖子,我深表歉意。我看到了一些类似的,但不够接近来回答这个问题。

【问题讨论】:

    标签: powershell ftp


    【解决方案1】:

    我创建了 PowerShell SFTP、FTP 和 FTPS 脚本。您必须从我的 github 下载它。我不使用任何第三方应用程序,因为我不信任它们。我使用的是 SFTP 部分的 REBEX.dll 和 FTP 和 FTPS 的 .Net WebRequest。

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

    如果您在理解代码时遇到问题,请告诉我。如果您不打算使用 Get-SFTP 功能,只需将其注释掉。

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 1970-01-01
      • 2011-03-19
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 2012-05-11
      相关资源
      最近更新 更多