4

I need to download a piece of text from an FTP server via PowerShell and get it as a string. After performing the required task, I need to upload it to the same server as a different file. The file must not be saved to the local storage at any point.

For a file on a regular HTTP server, the code would be (New-Object Net.WebClient).DownloadString($uri); for downloading and (New-Object Net.WebClient).UploadString($uri, $output);" for sending it to the server for processing via a POST request.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
sbrm1
  • 1,195
  • 3
  • 12
  • 25
  • try googling aound, this is a pretty common question. http://windowsitpro.com/windows/ftp-using-powershell – colsw Jun 13 '17 at 10:11
  • @ConnorLSW I checked but didn't find anything regarding manipulation of the file contents as a string. – sbrm1 Jun 13 '17 at 10:14
  • I'm not aware of any existing FTP implementation that entirely skips local storage, you'll need to configure jobs on the remote server to do this if you need that, or you can simply save it to a temp folder. – colsw Jun 13 '17 at 10:19
  • That too is possible, but I'd like to avoid it if possible. – sbrm1 Jun 13 '17 at 10:31
  • Is it possible to replace `DownloadFile` with `DownloadString` and `UploadFile` with `UploadString` in the following answer? https://stackoverflow.com/a/936941/5961780 – sbrm1 Jun 13 '17 at 10:42

1 Answers1

4

DownloadString and UploadString, as all WebClient methods, work for ftp:// URLs too:

By default, the .NET Framework supports URIs that begin with http:, https:, ftp:, and file: scheme identifiers.


So unless you need some fancy options, it's as simple as:

$webclient = New-Object System.Net.WebClient
$contents = $webclient.DownloadString("ftp://ftp.example.com/file.txt")

If you need to authenticate to the FTP server, either add credentials to the URL:

ftp://username:password@ftp.example.com/file.txt

Or use WebClient.Credentials:

$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential("user", "mypassword") 
$contents = $webclient.DownloadString("ftp://ftp.example.com/file.txt")
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • How to add authentication to it? – sbrm1 Jun 13 '17 at 10:47
  • Both methods give the following error: `Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (530) Not logged in." At line:1 char:1 + IEX $webclient.DownloadString('ftp://host2.bakop.com/im.ps1');` – sbrm1 Jun 13 '17 at 10:57
  • 1
    Works for me. Are you sure you have used correct credentials? Do you have an access to FTP server log? Or can you do FTP packet capture? – Martin Prikryl Jun 13 '17 at 11:05
  • The credentials are correct, I can use tools like ssh and scp, but the above code gives an error. Here is the WireShark Capture: http://imgur.com/XPWrZYK – sbrm1 Jun 13 '17 at 11:15
  • It's clear "login incorrect" => credentials are incorrect! Nothing we can help you with. Unless you prove that you can login with any FTP client using the same credentials (SSH and SCP are irrelevant here). – Martin Prikryl Jun 13 '17 at 11:23
  • I tried with another client, it worked, though I was asked for the password twice (on all attempts). I'm contacting the hosting provider for additional support. BTW thanks for the code, I will be needing it soon (after this issue is resolved). – sbrm1 Jun 13 '17 at 11:26
  • Show us a log file of the client. – Martin Prikryl Jun 13 '17 at 11:27
  • You have removed the Wireshark capture, so I cannot compare it + Stop posting log file as images + We cannot see the second password prompt in the "image" anyway. – Martin Prikryl Jun 13 '17 at 12:45