0

I am trying to upload a csv file via FTPS as I need SSL enabled but this code keeps throwing an error message (particularly at line $rs = $ftp.GetRequestStream()):

$username = "username"
$password = "password"
$Url = "ftp://hosturl"
$filename = "somefile.csv"
$usessl=$true

try {
    $uri = New-Object System.Uri($url)
    $ftp = [System.Net.FtpWebRequest]::Create($uri)
    $ftp = [System.Net.FtpWebRequest]$ftp
    $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $ftp.Credentials = new-object System.Net.NetworkCredential($username, $password)
    $ftp.UseBinary = $true
    $ftp.UsePassive = $true
    $ftp.EnableSsl = $usessl
    $content = [System.IO.File]::ReadAllBytes($filename)
    $ftp.ContentLength = $content.Length
    $rs = $ftp.GetRequestStream()
    $rs.Write($content, 0, $content.Length)
    'File Uploaded.'
    $rs.Close()
    $rs.Dispose()
}
catch{
    Throw $_    
}

Error Message:

Exception calling "GetRequestStream" with "0" argument(s): "System error."
greencode
  • 57
  • 1
  • 6
  • [Log file](https://stackoverflow.com/q/56220620/850848) please. – Martin Prikryl Jun 02 '20 at 19:51
  • are you sure about $url? should'nt it be: "ftps://...." – Bhaal22 Jun 03 '20 at 09:29
  • @JonathanMULLER No, .NET framework does not recognize `ftps://` protocol in URLs. See https://stackoverflow.com/q/30082054/850848 – Martin Prikryl Jun 03 '20 at 09:40
  • maybe have a look at this https://stackoverflow.com/questions/37606809/powershell-ftps-upload-to-box-com-using-passive-mode – Bhaal22 Jun 03 '20 at 09:48
  • @JonathanMULLER Good find. Seeing that the proposed answer actually does not do anything special, I guess that the probem is that OP (both here and there) try to read whole file to memory and the file may be too large for that. If that's the case, I suggest that the `CopyTo` (*"Advanced options"*) solution from my answer to [Upload files with FTP using PowerShell](https://stackoverflow.com/q/1867385/850848#46582183) might be a less obscure solution. – Martin Prikryl Jun 03 '20 at 09:55
  • @MartinPrikryl yeah indeed. nothing fancy. I will check out later also, I am quite curious why it epic fails. – Bhaal22 Jun 03 '20 at 09:58
  • I've improved the question found by @Bhaal22 and I have posted a (hopefully comprehensive) answer there. I'm closing this as a duplicate. – Martin Prikryl Jun 05 '20 at 11:22
  • cool @MartinPrikryl. – Bhaal22 Jun 05 '20 at 12:18

0 Answers0