Get-FtpFile.ps1

function Get-FtpFile()
{
    <#
         .SYNOPSIS
         下载ftp文件, Get-FTPItem 大文件太慢,不要用
          
         .DESCRIPTION
         Get-FtpFile -Username $FtpUsername -Password $FtpPassword -RemoteFile "ftp://thomasmaurer.ch/downloads/files/file.zip" -LocalFile "C:\Temp\file.zip"
     
    #>

    
     param (
      [Parameter(Mandatory)][string]$Username,
      [Parameter(Mandatory)][string]$Password,
      [Parameter(Mandatory)][string]$RemoteFile,
      [Parameter(Mandatory)][string]$LocalFile
    )
    

    # $Username = "FTPUSER"
    # $Password = "P@assw0rd"
    # $LocalFile = "C:\Temp\file.zip"
    # $RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"
    
    # GetFileSize
    $GetSizeRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
    $GetSizeRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
    $GetSizeRequest.Method = [System.Net.WebRequestMethods+Ftp]::GetFileSize
    $bytes_total = [long]$GetSizeRequest.GetResponse().ContentLength;
    
    # 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
    # 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
    $TotalReadLength = [long]0;
    do {
        $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
        $LocalFileFile.Write($ReadBuffer,0,$ReadLength)
        
        $TotalReadLength += $ReadLength;
        $PercentComplete = [int][math]::floor($TotalReadLength / $bytes_total * 100)
        Write-Progress -Activity "download in Progress" -Status "$PercentComplete% Complete:" -PercentComplete $PercentComplete
    }
    while ($ReadLength -ne 0)

    $LocalFileFile.Close();
    $FTPResponse.Close();
}