PowerShell FTPS 上传失败并出现“系统错误". [英] PowerShell FTPS upload fails with "System error."

查看:34
本文介绍了PowerShell FTPS 上传失败并出现“系统错误".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A client requires that we upload extracted data from our system to their box.com platform, rather than our normal SFTP utility. I have box.com credentials, and am aware they require FTPS not SFTP, and require passive mode. I've cribbed a fragment from ThomasMaurer's Powershell FTP Upload and Download script. Powershell version on my server is 4.0

#config 
$Username = "username@host.com"
$Password = "redactedpassword"
$LocalFile = "C:path	omyfile.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	omypowershellscript.ps1:28 char:1
+ $Run = $FTPRequest.GetRequestStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: () [], MethodInvocationException
+ FullyQualifiedErrorId: WebException

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

I also get downstream errors on calling the $FileContent.Length property and $run.close and $run.dispose().

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

Has anyone successfully automated to box (specifically) or to a passive implicit-ssl using only PowerShell 4.0 commands, and do you have a solid pattern I could reuse? Many thanks

推荐答案

我正在使用 System.Net.WebClient,支持 FTP over TLS.这可以通过在 PowerShell 中嵌入 C# 代码轻松实现:

I'm uploading files with a derived version of System.Net.WebClient, which supports FTP over TLS. This can easily be achieved by embedding C# code in PowerShell:

$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")

这篇关于PowerShell FTPS 上传失败并出现“系统错误".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆