-1

I would like to upload 2 files from an directory on my pc to an ftp server.

And this isnt working. Does someone have an idea? Thanks in advance!

Greetings

My new whole code:

$typeDefinition = @"
using System;
using System.Net;
public class FtpClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        FtpWebRequest ftpWebRequest = base.GetWebRequest(address) as FtpWebRequest;
        ftpWebRequest.EnableSsl = true;
        return ftpWebRequest;
    }
}
"@

Add-Type -TypeDefinition $typeDefinition

$LocalFilePath2 = "C:\M122\out"
$RemoteFileName2 = "****************"
$LocalFilePath2 = "******"
$ServerName2 = "*********"
$Username2 = "*******"
$Password2 = "*******************"

$ftpClient = New-Object FtpClient
$ftpclient.Credentials = New-Object System.Net.NetworkCredential($Username2, $Password2)

$files = Get-ChildItem $LocalFilePath2
foreach ($file in $files) {
    $uri2 = New-Object System.Uri("ftp://$ServerName2/$RemoteFileName2/$($file.name)")
    $ftpclient.uploadfile($uri2, $file.fullname)
}
Mark Elvers
  • 570
  • 2
  • 9
Joel_1930
  • 33
  • 1
  • 1
  • 6

1 Answers1

0

You need to create a $webclient object before trying to use it:

$webclient =  New-Object System.Net.WebClient

And you have something weird in your foreach loop as you create $ile but never use it. Furthermore, your remote filename is constant where it needs to be updated for each file. Perhaps you mean to do this:-

$files = Get-ChildItem $LocalFilePath2
foreach ($file in $files) {
    $uri2 = New-Object System.Uri("ftp://$ServerName2/$($file.name)")
    $webclient.uploadfile($uri2, $file.fullname)
}

If you need to support TLS then take a look at the this post Automate SSL FTP Upload via Powershell.

Take the code posted by h0r41i0 and use $ftpClient rather than $webclient.

$typeDefinition = @"
using System;
using System.Net;
public class FtpClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        FtpWebRequest ftpWebRequest = base.GetWebRequest(address) as FtpWebRequest;
        ftpWebRequest.EnableSsl = true;
        return ftpWebRequest;
    }
}
"@

Add-Type -TypeDefinition $typeDefinition

[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

$LocalFilePath2 = "****"
$ServerName2 = "****"
$Username2 = "****"
$Password2 = "****"

$ftpClient = New-Object FtpClient
$ftpclient.Credentials = New-Object System.Net.NetworkCredential($Username2, $Password2)

$files = Get-ChildItem $LocalFilePath2
foreach ($file in $files) {
    $uri2 = New-Object System.Uri("ftp://$ServerName2/$($file.name)")
    $ftpclient.uploadfile($uri2, $file.fullname)
}

Note that if the certificate on the remote server is not trusted then you need the ServerCertificateValidationCallback line to accept the certificate.

Mark Elvers
  • 570
  • 2
  • 9
  • I dont know why, but i dont get any error messages. But it isnt working. And the data (adress etc.) ist correct. Do you have any idea? – Joel_1930 Jan 28 '21 at 20:20
  • I've tested the code I posted against against my FTP server and it works. Perhaps `Write-Host $uri2 $file.fullname` in your loop and see if they correspond to values you're expecting, such as `ftp://abc.com/myfile.txt z:\path\to\file\myfile.txt`. And post your updated code... – Mark Elvers Jan 28 '21 at 20:25
  • $file.fullname does contain in the first half the correct path and in the second half the name of an file. And at uri the ip is correct but the second half (where the folders should be) there is also the name of a file in this ordner. Do you have an idea? – Joel_1930 Jan 28 '21 at 21:08
  • put the `Write-Host $uri2 $file.FullName` within the loop – Mark Elvers Jan 28 '21 at 21:16
  • `$RemoteFileName2` isn't used in the code as `$uri2` is overwritten before it is used. However, if I set `$ServerName2`, `$Username2` and `$Password2` to values in my environment and used some local files as input `$LocalFilePath2 = "c:\b\*.txt"` and the files were uploaded to my FTP server. What are you entering in `$LocalFilePath2` ? – Mark Elvers Jan 28 '21 at 21:21
  • this is my $LocalFilePath2 C:\M122\out – Joel_1930 Jan 28 '21 at 21:25
  • And if you run `gci c:\m122\out` does it print a list of files that you want to upload? – Mark Elvers Jan 28 '21 at 21:29
  • ive worte gci ... in the terminal of my ps script is that correct? – Joel_1930 Jan 28 '21 at 21:36
  • Yes, that's right. Try running `ftp $ServerName2` then manually enter your username/password at the prompts and then type `put c:\m122\out\your_first_file` and see if it uploads. – Mark Elvers Jan 28 '21 at 21:42
  • Then this happens after the first command: PS C:\M122> ftp $ServerName2 Verbindung mit ********** wurde hergestellt. 220---------- Welcome to Pure-FTPd [privsep] [TLS] ---------- 220-Local time is now 22:46. Server port: 21. 220-IPv6 connections are also welcome on this server. 220 You will be disconnected after 30 minutes of inactivity. 504 Unknown command And i cant enter the second command – Joel_1930 Jan 28 '21 at 21:46
  • From this error it looks to me like your FTP server requires the connection over TLS. I'll add some alternative code to my answer. – Mark Elvers Jan 28 '21 at 21:56
  • thanks for the code. Where cahn i enter $RemoteFileName (the folder) on the ftp server. Should this be on $Servername? – Joel_1930 Jan 28 '21 at 22:21
  • You could amend the uri like this System.Uri("ftp://$ServerName2/$RemoteFileName/$($file.name)" – Mark Elvers Jan 28 '21 at 22:30
  • sorry im very new to ps. Ive added my whole code now to my question. And it isnt working. Is there something missing. – Joel_1930 Jan 28 '21 at 22:39
  • I’ve updated the foreach loop of your code – Mark Elvers Jan 28 '21 at 22:48
  • Thanks for your help. But: there are again 4 error messages : WARNING: The generated type does not belong to any other methods or properties.And than 2 says system.uri isnt a cmdlet and two are saying Exception error of one of a WebClient rights. What should i do? I took exactly your script. – Joel_1930 Jan 28 '21 at 23:02
  • Hi, the warning is expected and fine. The system.uri message is bad. Please check what your code. It should read like this $uri2 = New-Object System.Uri(...) and not just system.uri on a line on its own. If that doesn’t fix it then wry have to pick this up in the morning. – Mark Elvers Jan 28 '21 at 23:18
  • The Uri Line looks like this: $uri2 = New-Object System.Uri("ftp://$ServerName2/$($file.name)") Okay, will be back tomorrow. – Joel_1930 Jan 28 '21 at 23:19
  • Is there an easier way because there are only two files in this folder? – Joel_1930 Jan 29 '21 at 10:48
  • Hi Joel, I've tested this morning. I've installed my own Pure FTP server to allow me to test using TLS. My first answer works for FTP without TLS. When TLS is enabled the second code works providing the certificate is trusted. If the server is not trusted you need to allow the certificate. I've updated my answer to include one additional line to just accept whatever certificate the server gives. – Mark Elvers Jan 29 '21 at 11:56