2

I've been wracking my brain on this issue and can't seem to fix it. I'm trying to upload a file to FTP using PSFTP.

The script I'm using:

#------------------------------------------------------
#local variables

$ftp_server = "SERVERNAME"
$ftp_path = "/FTPPATH/PATH"
$local = "C:\ftp\"
$local_in = Join-Path $local "In"
$local_out = Join-Path $local "Out"
$session = "my_ftp_session"

# set up credentials object
$username = "FFandP"
$password = Get-Content "$local_out\Credentials.txt" | ConvertTo-SecureString -AsPlainText -Force 
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password

Set-FTPConnection -Server $ftp_server -Credentials $cred -Session $session -KeepAlive -confirm -UseBinary

Get-ChildItem -Path $local_out |
% {

$ftp_file = "$ftp_path/$($_.Name)" # determine item fullname
Add-FTPItem -Path $ftp_file -LocalPath $_.FullName -Session $session -
}

# -------------------------------------------------

And the error I receive:

Add-FTPItem : 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:22 char:1
+ Add-FTPItem -Path $ftp_file -LocalPath $_.FullName -Session $session
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-FTPItem

I've tried running the Add-FTPitem command by itself, but I get the same error.

I can upload to the FTP using FileZilla. I have also tried removing the variables and using hard-coded paths; I get the same error.

Any ideas?

3 Answers 3

2

The answer in @Josh's comment solved it for me. Run Add-FTPItem with the -Overwrite parameter.

Add-FTPItem -Path $remotePath -LocaPath $myPath -Overwrite
Sign up to request clarification or add additional context in comments.

Comments

0

It took me a moment to figure out this problem, but here is my solution (I had the same problem).

When using Add-FTPItem the -Path parameter must not include the filename itself.

Add-FTPItem 
 -Path "ftp://SomeServer/SomeDir/"
 -LocalPath "C:\SomeFilename.ext"
 -Session $session

So in your example it should be:

Add-FTPItem -Path $ftp_path -LocalPath $_.FullName -Session $session

The filename will be added to the remote FTP path. In case you don't want to have the same name you must either rename the file locally first or remotely after.

1 Comment

What solved this issue for me was to specify the "-Overwrite" parameter. When Overwrite is set to false (default) it will first check to see if the file exists, when it doesn't exist the cmdlet throws an error because the event isn't properly handled. You can see some of this by specifying the "-Verbose" parameter.
-1

Change block

Get-ChildItem -Path $local_out | %{ .... }

to one line

Get-ChildItem -Path $local_out |  Add-FTPItem  -Path $ftp_path

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.