-1

I have the following code to upload a file to an FTP Server via powershell but it's giving me this error:



Code:

$Directory=”C:\test”

#FTP server configuration
$ftpserver = “ftp://ftpserver/”
$username = “user”
$password = “pw”

$webclient = New-Object System.Net.WebClient

$webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)

#Copy each file which type is *.tx*
foreach($file in (dir $Directory “*.txt*”)){
“Uploading $file…”
$uri = New-Object System.Uri($ftpserver+$file.Name)
$webclient.UploadFile($uri, $file.FullName)
}

Error:

Exception calling "UploadFile" with "2" argument(s): "Excepção durante um pedido WebClient."
At C:\Users\home\Desktop\test6.ps1:16 char:1
+ $webclient.UploadFile($uri, $file.FullName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
Carlos
  • 1
  • 2
  • Can you review FTP server logs? Maybe there would be more informative an error message. If you can't, start by printing out URI and filename and double-check there aren't anything funny like unescaped spaces or the like. – vonPryz Jun 26 '19 at 10:59
  • Did you check if the exception has any inner exception? + Do you get the same problem with a single-file upload (constant local path - no `foreach` loop)? + Fix the quotes in your code. – Martin Prikryl Jun 26 '19 at 11:49
  • Replace all the ugly curly 'smart-quotes' by straight ones. You get these things from copying off the internet or by typing the code in a text editor that is not suited for code. – Theo Jun 26 '19 at 12:36
  • The quotes are not the problem, since i've replace them with the straight ones and the error persists. – Carlos Jun 26 '19 at 13:02
  • We didn't say that the questes are the problem. But it makes your question look bad and makes it complicated for us to test your code. Do not forget to answer all our other questions. – Martin Prikryl Jun 26 '19 at 13:04
  • You can also have your PowerShell script generate [client-side log file](https://stackoverflow.com/q/56220620/850848). Just copy and paste the code from the answer to the linked question to the top of your script. It would probably help a lot with debugging the problem. – Martin Prikryl Jun 26 '19 at 14:06

1 Answers1

0

Try it like so:

$Directory = "C:\test"

#FTP server configuration
$ftpserver = "ftp://ftpserver/"
$username = "user"
$password = "pw"
$ftpserverURI = New-Object -TypeName System.Uri -ArgumentList $ftpserver, [System.UriKind]::Absolute

$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $username, $password

#Copy each file which type is *.tx*
Get-ChildItem $Directory -Filter *.txt* | ForEach-Object {
    Write-Host "Uploading $($_.FullName)..."
    $uri = New-Object -TypeName System.Uri -ArgumentList $ftpserverURI, $_.Name
    $webclient.UploadFile($uri, [System.Net.WebRequestMethods+Ftp]::UploadFile, $_.FullName)
}

The differences are that I'm making System.Uri combine the path instead of relying on string concatenation, and I'm telling WebClient.UploadFile() the method to use when uploading the file.

If this doesn't work, then I agree with the comments that you should examine the server logs. If you can't, then try it against a server that you can see the logs for. Alternately, you may want to try to use WinSCP, which is also scriptable with PowerShell or with a custom script file. WinSCP has the advantage of supporting FTP, FTPS, and SFTP, as well. The .Net WebClient only natively supports plain FTP, as far as I'm aware.

As far as smart quotes, they work just fine on Windows PowerShell (<= v5.x), but they don't work at all on PowerShell Core (v6+). I would avoid using them to make your code more portable and more future proof.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • `[System.Net.WebRequestMethods+Ftp]::UploadFile` is the default for `ftp://` URLs, so it should not make any difference. – Martin Prikryl Jun 26 '19 at 14:05
  • It throws a bigger error `Exception calling "UploadFile" with "3" argument(s): "Excepção durante um pedido WebClient." At C:\Users\TeixeiraC\Desktop\TEMP\ps\test6.ps1:17 char:5 + $webclient.UploadFile($uri, [System.Net.WebRequestMethods+Ftp]::U ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException` – Carlos Jun 26 '19 at 15:01
  • So finally respond to ours comments to your question above. You won't get any help otherwise. We are not magicians. I hope you can see that the error message you keep posting does not contain any useful information. So how can we tell, what's wrong? – Martin Prikryl Jun 26 '19 at 15:42