-1

I want to use powershell script to upload images on FTP server but I keep getting error An exception occurred during a WebClient request, not sure what's wrong with my script. Any help please?

$FTPHost = "ftp://xxxx/"
$FTPUser = "xxxx"
$FTPPass = "xxxxx"

$source = "C:\Images"

$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object System.Net.NetworkCredential($FTPUser, $FTPPass)

$files = get-childitem $source -recurse -force

foreach ($file in $files)
{
    Write-Host "Uploading $file"
    $webClient.UploadFile("$FTPHost", $file.Name)
}

$webClient.Dspose()
I bex
  • 31
  • 1
  • 9
  • 1
    What is the error you are getting? – gattsbr Jul 28 '16 at 17:28
  • [Check this topic](https://stackoverflow.com/questions/1867385/upload-files-with-ftp-using-powershell/55851496#55851496) "Upload files with FTP using PowerShell" it's the same! –  Sep 13 '19 at 15:57

1 Answers1

0

Try using UploadFile this way:

$webClient.UploadFile("$FTPHost" + "/" + $file.Name, "STOR", $file.FullName)

You have to give it the full filename and also where to store it.

Peter Hahndorf
  • 10,767
  • 4
  • 42
  • 58
  • I just tried that and got a new error Exception calling "UploadFile" with "3" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." At C:\FTPImages.ps1:24 char:5 + $webClient.UploadFile("$uri" + "/" + $file.Name, "STOR", $file.FullName) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException – I bex Jul 29 '16 at 09:17
  • Peter your suggestion actually worked. There was an extra slash causing the issue. .... thanks – I bex Jul 29 '16 at 10:09