PowerShell ftps上传失败,并显示“系统错误。”

问题描述 投票:2回答:3

问题:

客户要求我们从系统中将提取的数据上传到他们的box.com平台,而不是正常的SFTP实用程序。我具有box.com凭据,并且知道它们需要FTPS而不是SFTP,并且需要被动模式。我抄写了ThomasMaurer's Powershell FTP Upload and Download script的片段。我服务器上的Powershell版本是4.0

代码片段为:

#config 
$Username = "[email protected]"
$Password = "redactedpassword"
$LocalFile = "C:\path\to\my\file.csv"
$RemoteFile = "ftp://ftp.box.com:990/file.csv"
#Create FTPWebRequest
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
$FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$FTPRequest.UseBinary = $true
$FTPRequest.UsePassive = $true
#read file for upload
$FileContent = gc -en byte $LocalFile
$FTPRequest.ContentLength = $FileContent.Length
#get stream request by bytes
$run = $FTPRequest.GetRequestStream()
$run.Write($FileContent,0,$FileContent.Length)
#cleanup
$run.Close()
$run.Dispose()

错误:

Exception calling "GetRequestStream" with "0" argument(s): "System error." At C:\path\to\my\powershellscript.ps1:28 char:1
+ $Run = $FTPRequest.GetRequestStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: () [], MethodInvocationException
+ FullyQualifiedErrorId: WebException

我在调用$FileContent.Length属性以及$run.close$run.dispose()时也遇到下游错误。

有没有人仅使用PowerShell 4.0命令成功地自动装箱(特别是装箱)或被动式隐式ssl,您是否有可以重用的可靠模式?非常感谢

powershell ssl ftp ftpwebrequest ftps
3个回答
2
投票

我正在上传具有System.Net.WebClient派生版本的文件,该版本支持基于TLS的FTP。通过在PowerShell中嵌入C#代码,可以轻松实现这一点:

$typeDefinition = @"
using System;
using System.Net;
public class FtpClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        FtpWebRequest ftpWebRequest = base.GetWebRequest(address) as FtpWebRequest;
        ftpWebRequest.EnableSsl = true;
        return ftpWebRequest;
    }
}
"@

Add-Type -TypeDefinition $typeDefinition
$ftpClient = New-Object FtpClient
$ftpClient.UploadFile("ftp://your-ftp-server/yourfile.name", "STOR", "C:\YourLocalFile.name")

1
投票

可能为时已晚,无法对原始提问者有用,但我发现另一个答案对我有用:Cyril Gupta's answer to Upload files with ftp using powershell

这里是我的修订版,包括URL编码(因为box.com用户名是包含“符号”的电子邮件地址:]

## https://stackoverflow.com/a/2485696/537243
## User comment complains can't turn off passive mode, 
## but that is exactly what we want here!
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null

# config 
$Username = "[email protected]"
$Password = "s3cr3tpAssw0rd"
$Servername = "ftp.box.com"

# This is what we need URI it to look like:
#   ftp://foo%40bar.com:[email protected]/
$baseURI = "ftp://$([System.Web.HttpUtility]::UrlEncode($Username)):$([System.Web.HttpUtility]::UrlEncode($Password))@$($Servername)"

$LocalFile = "C:\tmp\to_upload\data.csv"
$RemoteFile = "date.csv"
$ftpURI = "$($baseURI)/$($RemoteFile)"

Write-output "ftp uri: $($ftpURI)";

$webclient = New-Object -TypeName System.Net.WebClient;
$ftpURI = New-Object -TypeName System.Uri -ArgumentList $ftpURI; #"convert" it
$webclient.UploadFile($ftpURI, $LocalFile);

Write-output  "Uploaded $($LocalFile) ... "; # of course since we didn't use try/catch or other error dectection this is a bit presuming.

还应注意,此示例使用普通FTP,而不是FTPS或SFTP。


0
投票

@ h0r41i0的答案通过使用WebClient解决了该问题。但是由于WebClient内部使用(Ftp)WebRequest,因此它本身不能成为解决方案。

我将假定发生“系统错误”,因为任一OP都试图通过不安全的连接连接到安全端口(990)。

或者因为文件太大而OP代码试图将其全部读取到内存中:

$FileContent = gc -en byte $LocalFile

[无论哪种情况,都没有理由放弃FtpWebRequest。只需使用安全连接(FtpWebRequest.EnableSsl)。一种将文件中的数据馈送到FTP流的有效方法,例如Stream.CopyTo

Stream.CopyTo

有关其他选项,请参见$request = [Net.WebRequest]::Create("ftp://ftp.example.com/remote/path/file.zip") $request.Credentials = New-Object System.Net.NetworkCredential("username", "password") $request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile $request.EnableSsl = $True $fileStream = [System.IO.File]::OpenRead("C:\local\path\file.zip") $ftpStream = $request.GetRequestStream() $fileStream.CopyTo($ftpStream) $ftpStream.Dispose() $fileStream.Dispose()

尽管请注意,.NET框架不支持隐式TLS(990的典型用法)。仅显式TLS。但是,对显式TLS的支持更为常见。参见Upload files with FTP using PowerShell

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