用于从FTPS服务器下载文件的Powershell脚本

问题描述 投票:1回答:1

我看过混合的帖子,但最近没有确切的定义。我正在运行Filezilla服务器FTPS显式TLS。我需要一个Powershell脚本,该脚本在运行时将通过Internet安全地连接到我的服务器并下载特定文件。减去TLS,下面的代码看起来不错吗,有人可以帮我完成它吗?我认为我需要的所有命令都将从此处https://winscp.net/eng/docs/library_sessionoptions#ftpsecure

    # Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ftp.example.com"
    UserName = "user"
    Password = "mypassword"
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Download files
    $session.GetFiles("/directory/to/download/*", "C:\target\directory\*").Check()
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}    
powershell download filezilla ftps
1个回答
0
投票

没有理由从头开始执行此操作,因为在网络上有很多示例可以直接使用或按原样使用。

查询的基础可以看作是此问答的重复...

PowerShell FTP download files and subfolders

...以及WinSCP作者的可接受答案...

$localFilePath = Join-Path $localPath $name
        $fileUrl = ($url + $name)

        if ($permissions[0] -eq 'd')
        {
            if (!(Test-Path $localFilePath -PathType container))
            {
                Write-Host "Creating directory $localFilePath"
                New-Item $localFilePath -Type directory | Out-Null
            }

            DownloadFtpDirectory ($fileUrl + "/") $credentials $localFilePath
        }
        else
        {
            Write-Host "Downloading $fileUrl to $localFilePath"

            $downloadRequest = [Net.WebRequest]::Create($fileUrl)
            $downloadRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
            $downloadRequest.Credentials = $credentials

            $downloadResponse = $downloadRequest.GetResponse()
            $sourceStream = $downloadResponse.GetResponseStream()
            $targetStream = [System.IO.File]::Create($localFilePath)
            $buffer = New-Object byte[] 10240
            while (($read = $sourceStream.Read($buffer, 0, $buffer.Length)) -gt 0)
            {
                $targetStream.Write($buffer, 0, $read);
            }
            $targetStream.Dispose()
            $sourceStream.Dispose()
            $downloadResponse.Dispose()
        }
    }
}
Use the function like:

$credentials = New-Object System.Net.NetworkCredential("user", "mypassword") 
$url = "ftp://ftp.example.com/directory/to/download/"
DownloadFtpDirectory $url $credentials "C:\target\directory"

例如,对于WinSCP .NET程序集,您可以下载整个目录只需一次调用Session.GetFiles:

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ftp.example.com"
    UserName = "user"
    Password = "mypassword"
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Download files
    $session.GetFiles("/directory/to/download/*", "C:\target\directory\*").Check()
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}  

[WinSCP在内部,如果服务器支持,则使用MLSD命令。如果没有,它将使用LIST命令并支持数十种不同的列表格式。

Session.GetFiles方法默认是递归的。

(我是WinSCP的作者)

© www.soinside.com 2019 - 2024. All rights reserved.