January 28, 2021 at 12:53 pm
Does anyone have a ftp upload script they could share that has the following requirements.
1) Prompt user for folder where files to upload exist.
2) Verify that folder exists and has files. If not stop script and have user verify
3) Upload all files from folder
4) Keep a Log of the files sent and size of the files.
predefined in script
user
password
ftp site
Dest Folder
Thanks.
January 28, 2021 at 3:38 pm
This isn't quite what you are after, but it's not far off, and I am sure you'll be able to modify it for your needs (such as add a for-each
loop for each file in a folder. This is a modified version of the solution found here I use, which has logging, the ability to use SSL and (of course) be parametrised:
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String]$Username,
[Parameter(Mandatory=$true)]
[String]$Password,
[Parameter(Mandatory=$true)]
[String]$LocalFile,
[Parameter(Mandatory=$true)]
[String]$RemoteFile,
[Parameter(Mandatory=$false)]
[System.Boolean]$EnableSSL = $false
)
# Taken from https://www.thomasmaurer.ch/2010/11/powershell-ftp-upload-and-download/
# Create a FTPWebRequest
Write-Host "Creating FTP 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.KeepAlive = $false
$FTPRequest.EnableSsl = $EnableSSL
# Read the File for Upload
#$FileContent = gc -en byte $LocalFile
Write-Host "Reading File"
$FileContent = [System.IO.File]::ReadAllBytes("$LocalFile")
$FTPRequest.ContentLength = $FileContent.Length
# Get Stream Request by bytes
Write-Host "Sending File"
Add-Content -Path "C:\Powershell\Logs\upload.log" -Value "$(Get-Date) - Submission started of file $LocalFile"
try {
$Run = $FTPRequest.GetRequestStream()
$Run.Write($FileContent, 0, $FileContent.Length)
Write-Host "Sending Complete"
} catch {
Write-Host "Sending failed"
Write-Error $_
Add-Content -Path "C:\Powershell\Logs\upload.log" -Value "$(Get-Date) - Submission failed of file $LocalFile"
Add-Content -Path "C:\Powershell\Logs\upload.log" -Value "$(Get-Date) - Error: $_"
Exit 1
}
#FtpWebResponse response = (FtpWebResponse) request.GetResponse();
#$response = [System.Net.FtpWebResponse]::request.GetResponse()
#Write-Host("Append status: " + $response.StatusDescription);
Add-Content -Path "C:\Powershell\Logs\upload.log" -Value "$(Get-Date) - Submission complete of file $LocalFile"
# Cleanup
$Run.Close()
$Run.Dispose()
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 28, 2021 at 4:08 pm
After connecting to FTP site how do you do a change directory command, and can you just point to a folder to
deposit the files from local, not having to give them a name just inherent the local file name.
Thanks for reply.. I like the logging
January 28, 2021 at 4:18 pm
After connecting to FTP site how do you do a change directory command, and can you just point to a folder to deposit the files from local, not having to give them a name just inherent the local file name.
Thanks for reply.. I like the logging
There is a parameter for the destination: $RemoteFile
. Again, you should be able to change that simply enough to accept and folder and concatenate the file's name to it in your For-Each
loop.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 28, 2021 at 5:42 pm
I'm testing with remotefile but have something wrong
$RemoteFile = "ftp://ftp.xxx.com/filedrop/test.txt"
Exception calling "GetRequestStream" with "0" argument(s): "The requested URI is invalid for this FTP command."
At line:18 char:1
+ $Run = $FTPRequest.GetRequestStream()
Thx.
January 28, 2021 at 7:02 pm
I ran script as delivered, and seems like my remotefile something is wrong.
"ftp://ftp.xxx.com/filedrop/test.txt"
If I open a cmd window and feed param in just like script it works.
Thanks.
January 28, 2021 at 7:03 pm
error:
Exception calling "GetRequestStream" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
January 29, 2021 at 12:05 pm
Thanks for replies, the script is working there was a security error on ftp site causing issues. The question I have now is how do you
issue a Change Directory command?
When I first hit the site I need to be able to CD to a test or prod directory.
Thanks again..
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply