# GetRequestStream error

**URL:** <https://forums.powershell.org/t/getrequeststream-error/24512>\
**Category:** PowerShell Help\
**Created:** [July 30, 2024, 3:29pm UTC](https://forums.powershell.org/t/getrequeststream-error/24512 "2024-07-30T15:29:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darkveemon1](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Darkveemon1](https://forums.powershell.org/u/Darkveemon1)\
**Post date:** [July 30, 2024, 3:29pm UTC](https://forums.powershell.org/t/getrequeststream-error/24512/1 "2024-07-30T15:29:43Z")

</div>

Hi, I’m using Power shell 4 and need to send some files via FTP, but I get this error:

Exception calling “GetRequestStream” with “0” argument(s): “System Error.”

I looked online and the problem seems related to the file path I’m using, but if I print the Uri it is correct.

In Powershell 5 it seems to work, but on this particular server I have PS 4.0

Thank you in advance

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [July 30, 2024, 6:17pm UTC](https://forums.powershell.org/t/getrequeststream-error/24512/2 "2024-07-30T18:17:57Z")

</div>

Hi Darkveemon1,

Without a code sample to review and/or test with, I doubt anyone will be able to offer much assistance. Please add a minimal reproducible example and be sure to format it with the \</\> formatting option. (may be hidden under the gear icon depending on your screen size/settings)

---

<div class="post-metadata">

**Author:** ![Darkveemon1](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Darkveemon1](https://forums.powershell.org/u/Darkveemon1)\
**Post date:** [July 30, 2024, 8:28pm UTC](https://forums.powershell.org/t/getrequeststream-error/24512/3 "2024-07-30T20:28:28Z")

</div>

Ok sorry, I was a bit in a rush and had only access to my phone, basically I have a series of csv files in a folder on a server, I have to sand them via FTPS to a folder in another server (I’ve already tested the connection with Winscp and it is ok), but in powershell 4.0 I get the error above. Here there is my code (wich seems to work on Powershell 5, a collegue tested it, but on that particular server I only have Powershell 4.0 and cannot update it).

Thank you in advance

```auto
# FTPS server parameters
$ftpHost = "xxx.xxx.xxx.xxx" #ip of the server with the destination folder
$ftpPort = 21
$ftpUser = "username" # username
$ftpPass = "psw" # password

# Local folder with the csv files I have to send
$localFolderPath = "C:\Source_Folder"

# Destination folder where the files will be copeid to
$remoteFolderPath = "/Dest_folder"

function Upload-FileToFtp {
    param (
        [string]$localFilePath,
        [string]$remoteFilePath
    )

    try {
        # FTP request creation
        $uri = New-Object System.Uri("ftp://${ftpHost}:${ftpPort}${remoteFilePath}")
        $ftpRequest = [System.Net.FtpWebRequest]::Create($uri)
        $ftpRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
        $ftpRequest.Credentials = New-Object System.Net.NetworkCredential($ftpUser, $ftpPass)
        $ftpRequest.EnableSsl = $true
        $ftpRequest.UseBinary = $true
        $ftpRequest.KeepAlive = $false

        # Reading of local file and subsequent upload on FTP flow
        $ftpRequest.ContentLength = (Get-Item -Path $localFilePath).length
        $fileContent = [System.IO.File]::ReadAllBytes($localFilePath)
        $requestStream = $ftpRequest.GetRequestStream()
        $requestStream.Write($fileContent, 0, $fileContent.Length)
        $requestStream.Close()

        # Wait for FTP server answer
        $ftpResponse = $ftpRequest.GetResponse()
        $ftpResponse.Close()
        Write-Host "Loading of $localFilePath done."
    } catch {
        Write-Host "Error loading ${localFilePath}: $_"
    }
}

# Get all the CSV files from my source folder
$csvFiles = Get-ChildItem -Path $localFolderPath -Filter *.csv

foreach ($csvFile in $csvFiles) {
    # Path of the source folder
    $localFilePath = $csvFile.FullName

    # Path of the file in my destination folder
    $remoteFilePath = ${remoteFolderPath} +"/" + $csvFile.Name

    # Loading of the file on FTPS server
    Upload-FileToFtp -localFilePath $localFilePath -remoteFilePath $remoteFilePath
   
}

Write-Host "Loading done."

```

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [July 30, 2024, 9:07pm UTC](https://forums.powershell.org/t/getrequeststream-error/24512/4 "2024-07-30T21:07:36Z")

</div>

unfortunately I don’t have access to a 2012R2 box to reproduce this with. And “System error” is pretty generic on that GetRequestStream message.  
If it were me, I’d get on that server, and just start selectively executing the code from the function one line at a time until you can recreate the error message. Maybe more details will be revealed.

---

<div class="post-metadata">

**Author:** ![Darkveemon1](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Darkveemon1](https://forums.powershell.org/u/Darkveemon1)\
**Post date:** [July 31, 2024, 4:56am UTC](https://forums.powershell.org/t/getrequeststream-error/24512/5 "2024-07-31T04:56:23Z")

</div>

I understand, does the code I put in the last message seem correct to you? Or there is something clearly wrong?

If I print the Uri after generating it it is correct.

Btw, how did you figure I was using Windows Server 2012?

Ty

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [July 31, 2024, 2:33pm UTC](https://forums.powershell.org/t/getrequeststream-error/24512/6 "2024-07-31T14:33:07Z")

</div>

Basically the only time you see version 4 is 2012/2012R2. 2016/windows 10 comes with 5.1 and no one in their right mind would upgrade older versions to 4, as WMF 5.1 has been available since W2K8 R2/Windows 7 days.

**EDIT**  
You should also upgrade to 5.1 by installing the WMF 5.1 package. Some IT people suggest keeping it version 4 if you’re running Exchange on the server, but I’ve not seen an issue with exchange caused by WMF 5.1 Note there is a minimum .NET version WMF requires, I want to say it is 4.7.2 but that’s just a guess.

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [July 31, 2024, 6:08pm UTC](https://forums.powershell.org/t/getrequeststream-error/24512/7 "2024-07-31T18:08:43Z")

</div>

> [@Darkveemon1](#):
>
> I understand, does the code I put in the last message seem correct to you? Or there is something clearly wrong?

it did look correct to me, and i found some other examples online of people using the same .NET method to do this. I myself have only ever script SFTP (vs FTPS) and I used Posh-SSH module for that.

> [@Darkveemon1](#):
>
> Btw, how did you figure I was using Windows Server 2012?

like @krzydoug said, I googled which version’s of Windows shipped with Powershell v4. What I didn’t know was that you could potentially upgrade to 5.1 on that server. **That** might actually be the avenue to take since you said this code works on other machines running 5.1.

Otherwise, the only thing I would know how to do from here is manually execute each line of code from that function, until the error generates, and then try to either explore the error or, gosh, i don’t know.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 30, 2024, 6:08pm UTC](https://forums.powershell.org/t/getrequeststream-error/24512/8 "2024-08-30T18:08:56Z")

</div>

Closed on August 30, 2024, 6:08pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
