I am trying to download files from another company's server using the specs they provided and bring them to my local folder but its not working. I tested just being able to list the directory and that works.
# Function to get directory listing
Function Get-FTPFileList {
Param (
[System.Uri]$server,
[string]$username,
[string]$password,
[string]$directory
)
try
{
#Create URI by joining server name and directory path
$uri = "$server$directory"
#Create an instance of FtpWebRequest
$FTPRequest = [System.Net.FtpWebRequest]::Create($uri)
#Set the username and password credentials for authentication
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($username, $password)
#Set method to ListDirectoryDetails to get full list
#For short listing change ListDirectoryDetails to ListDirectory
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
#Get FTP response
$FTPResponse = $FTPRequest.GetResponse()
#Get Reponse data stream
$ResponseStream = $FTPResponse.GetResponseStream()
#Read data Stream
$StreamReader = New-Object System.IO.StreamReader $ResponseStream
#Read each line of the stream and add it to an array list
$files = New-Object System.Collections.ArrayList
While ($file = $StreamReader.ReadLine())
{
#Get-Content $file.FullName
[void] $files.add("$file")
}
}
catch {
#Show error message if any
write-host -message $_.Exception.InnerException.Message
}
#Close the stream and response
$StreamReader.close()
$ResponseStream.close()
$FTPResponse.Close()
Return $files
}
#Set server name, user, password and directory
$server = 'ftp://1.1.1.1/' #Cant show the real one
$username = 'username'
$password = 'pswd'
$directory ='folderpath/*.810OUT'
#Function call to get directory listing
$filelist = Get-FTPFileList -server $server -username $username -password $password -directory $directory
#Write output
Write-Output $filelist
This works and through FileZilla I saw I full permissoin on the folder and subequently the files within it.
But then when I try the following:
$sourceuri = 'ftp://1.1.1.1//Orders/OutboxFolder/*.810OUT'
$targetpath = "C:\Users\myname\Downloads\test"
$username = "username"
$password = "pswd"
# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)
# set the request's network credentials for an authenticated connection
$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 to the server
$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
$targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024
# loop through the download stream and send the data to the target file
do{
$readlength = $responsestream.Read($readbuffer,0,1024)
$targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)
$targetfile.close()
It doesnt work. I get these errors
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
At line:18 char:1
+ $ftpresponse = $ftprequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
New-Object : Exception calling ".ctor" with "2" argument(s): "Access to the path 'C:\Users\myname\Downloads\test' is denied."
At line:24 char:15
+ ... argetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Crea ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I am new to trying to do this with Powershell so I am not sure what to do. Also the port isn't used here by the specs given specify Port 21. And note that I don't want to recursively download. Just wants in that one location.