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)
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
# 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."
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.
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.
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.
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.