PowerShell FTP Files

I am trying to move my files that I back nightly onto a local server to one of my hosted servers.  I have created this file in PowerShell script but am getting the below errors.  The server on the other end is 2K3 running FieZilla.

# locate all backup files
Foreach($File in Get-Childitem "\\ServerName\ServerBackups\SQL\db\" -recurse)
{
      # upload all the files
      
    $ftp = "ftp://username_backup:password@compass.mycompany.com/" + $File
    "ftp url: $ftp"

    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)

     "Uploading $File..."
      $webclient.UploadFile($uri, $File)
}

And here is the error I get

ftp url: ftp://username_backup:password@compass.mycompany.com/backup_201104081800.bak
Uploading backup_201104081800.bak...
Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:13 char:23
+     $webclient.UploadFile <<<< ($uri, $File)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
ftp url:ftp://username_backup:password@compass.mycompany.com/backup_201104101800.bak
Uploading backup_201104101800.bak...
Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:13 char:23
+     $webclient.UploadFile <<<< ($uri, $File)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
iaakadminAsked:
Who is Participating?
 
AlexPaceCommented:
You could use the Task Scheduler to launch the Robo-FTP script below to accomplish the same goals.  This logic recurses into any subfolders and upload the files but does not recreate the directory structure.  This could be an issue if you have two subfolders containing files with the same name because one of them will be overwritten.  If you actually DO want to recreate the folder structure on the remote site just remove the /flat option on the SENDFILE command.
WORKINGDIR "\\ServerName\ServerBackups\SQL\db\"
IFERROR!= $ERROR_SUCCESS GOTO done  ; bail out if network path unreachable
FTPLOGON "ftp.mycompany.com" /user="backup" /pw="password@compass.mycompany.com"
SENDFILE "*.bak" /subdirs /flat  ; upload files in subfolders but not folders
FTPLOGOFF
:done
exit

Open in new window

Even if you don't want to do it with Robo-FTP you'll still need to consider how you want to handle the subfolders you recurse into and what will happen if multiple subfolders contain files with the same name.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.