Powershell error FTP Upload Script

jverasql
jverasql used Ask the Experts™
on
Hello,
 I am new to Powershell. I am looking for a script to perform an ftp upload.  I found the script below.   I made changes to the variable values. The variables values below are not mine.  

# Config
$Username = "FTPUSER"
$Password = "Passw0rd"
$LocalFile = "C:\Temp\file.zip"
$RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"
 
# Create a FTPWebRequest
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create)
[byte[]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
      do {
            $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
            $LocalFileFile.Write($ReadBuffer,0,$ReadLength)
      }
      while ($ReadLength -ne 0)

When I run I get the following error shown in the attachment Ftp Powershell errorwhich just keeps repeating.  I guess due to the loop.  The file does not transfer. Please let me know if any additional infomation is needed.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
$responseStream  var is empty.

Author

Commented:
Thank you.  How do I correct this?  Does this mean it cannot find the find locally to transfer?
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018
Commented:
You do need to open it on PowerShell ISE, set a breakpoint and check it line by line, it's a debug process and make sure the variable is not empty, so it can work the binary part (loop) and continue the progress of the script.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Global Support Services Engineer
Commented:
You're using the script to download a file.  If you took this code from this website, you can read in the comments below the post that the author made a typo, however, you can also see in the code you're using that the FTPWebRequest method is DownloadFile.

Use this code instead:
# Config
$Username = "FTPUSER"
$Password = "P@assw0rd"
$LocalFile = "C:\Temp\file.zip"
$RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"

# Create FTP Rquest Object
$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 the 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()

Open in new window

Author

Commented:
I apologize for the delay.  The Expert Comment by:dsuyemoto is very helpful.  Please keep open I will try the solution and respond ASAP next day or two.

Again, I apologize.  

Thank you.
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
Answered

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial