Upload files to an FTP with PowerShell

0

Good morning,

I'm trying to upload files to an FTP with PowerShell but it gives me an error, if you can bring some light I would appreciate it very much.

$ftp = "ftp://192.168.0.2/" 
$user = "user" 
$pass = "pass"  
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
$item="C:/Pruebas/origen/prueba.txt"
$uri = New-Object System.Uri($ftp+$item.Name) 
$webclient.UploadFile($uri, $item.FullName) 

Excepción al llamar a "UploadFile" con los argumentos "2": "Excepción durante una solicitud WebClient."
En línea: 8 Carácter: 1
+ $webclient.UploadFile($uri, $item.FullName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

Any ideas?

Thank you.

    
asked by genesis 20.10.2017 в 09:52
source

1 answer

0

I think the error comes from the variable where you define the file to upload:

$item="C:/Pruebas/origen/prueba.txt"

And then you use the Name and FullName properties:

$uri = New-Object System.Uri($ftp+$item.Name) 
$webclient.UploadFile($uri, $item.FullName) 

Since being a string , it is not possible to invoke these properties. The easiest thing is to obtain the required values in the following way:

$itemName = "prueba.txt"
$itemFullName = "C:\Pruebas\origen\$itemName"

And replace the code with the new variables:

$ftp = "ftp://192.168.0.2/" 
$user = "user" 
$pass = "pass"  
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
$itemName = "prueba.txt"
$itemFullName="C:/Pruebas/origen/$itemName"
$uri = New-Object System.Uri($ftp+$itemName) 
$webclient.UploadFile($uri, $itemFullName)

It may not be the best way, but it should work.

    
answered by 15.01.2018 в 02:18