We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

Novice PowerShell scripter having issues

54 Views
Last Modified: 2020-12-28
Per suggestion from "Alex [***Alex140181***] " I am attempting using a powershell script to automate an FTP download.
This is to download files, regularly which may update throughout the day, but which will always have the following naming convention "YYMMDD.asc"

I attempted to run using the sample at the link provided,
https://www.thomasmaurer.ch/2010/11/powershell-ftp-upload-and-download/ 
but I am afraid I am just too new to this to see what I am doing wrong...I receive a "You cannot call a method on a null-valued expression. At D:\[..].ps1.23 char:1 +$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
and $ResponseStream.Read($ReadBuffer0,1024)
"See attached Image"
Script is below

# Config
$Username = "<username>"
$Password = "<password>"
$LocalFile = "<Local path>\@Y@M@D.asc"
$RemoteFile = "<ftp site>/filename.asc"
 
# 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) 
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 4 Answers and 8 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE