0

I will like upload a file to SFTP Filezilla but I have an error message and I can not solve my problem

$ftp = "Address"
$Username = "username" 
$Password = "password" 
$Localdir = "address\test.docx"

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password)

foreach ($item in (dir $Localdir "*.docx")) {

"Uploading $item..."
    $uri = New-Object System.Uri($ftp+$item.Name) 
    Write-Output $uri,$item.FullName
    $webclient.UploadFile($uri, $item.FullName)  
}

When I execute my script, I have an error message :

Exception when calling "UploadFile" with "2" argument (s): "An exception occurred during a WebClient request. To the character C: \ Users \ PCA \ Downloads \ powershell \ test4 - to see.ps1: 15: 5 + $ webclient.UploadFile ($ ftp, $ item) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException

How resolve this problem ? (I execute this script with on windows 10 and the version powershell is 5.1)

pcarrey
  • 39
  • 2
  • 2
  • 8
  • 1
    Change `$webclient.UploadFile($ftp, $item)` to `$webclient.UploadFile($ftp, $item.FullName)` –  Nov 14 '18 at 09:03
  • Okay, I changed that line. The problem remains the same – pcarrey Nov 14 '18 at 09:05
  • 2
    Please try to get the error message [in English](https://stackoverflow.com/q/19869627) or provide your own translation. My French is a bit too rusty for this. – vonPryz Nov 14 '18 at 09:12
  • I've translate the error message in english :) – pcarrey Nov 14 '18 at 09:28
  • You should catch the exception and look at the InnerException to see if you get any more info there: `try { ... } catch { $_.Exception.InnerException }` – marsze Nov 14 '18 at 09:33
  • @pcarrey take a look at this https://community.spiceworks.com/topic/654445-sftp-upload-a-file-to-a-host-via-powershell – HariHaran Nov 14 '18 at 11:15
  • @HariHaran I have look the topic but I also en error message – pcarrey Nov 14 '18 at 13:30
  • @marsze I do not understand, how should I do ? – pcarrey Nov 14 '18 at 13:32
  • 1
    @pcarrey Lookup exception handling in Powershell. Use the code I posted. (Paste all of *your* code there were the three dots are.) The run it and look at the output. – marsze Nov 14 '18 at 13:51
  • @marsze at the end of execution, i have a message : An exception occurred during a WebClient request. – pcarrey Nov 14 '18 at 14:02
  • Shouldn't you be doing `$webclient.UploadFile($uri, $item.FullName)` ? – Theo Nov 14 '18 at 14:24
  • @Theo I changed the line `$webclient.UploadFile($uri, $item.FullName)` and the problem remains the same – pcarrey Nov 14 '18 at 14:29
  • 1
    It says [here](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadfile?view=netframework-4.7.2) that a WebException is thrown when _The URI formed by combining BaseAddress, and address is invalid._ Check the `$uri` variable. Is that a valid [WebClient.BaseAddress](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.baseaddress?view=netframework-4.7.2#System_Net_WebClient_BaseAddress)? – Theo Nov 14 '18 at 14:35
  • @Theo Yes my base.address is correct – pcarrey Nov 14 '18 at 14:42

0 Answers0