Powershell – How to upload using FTP in Powershell, behind a proxy

ftppowershellproxy

I am attempting to use FTP in Powershell to upload a file. I am using FtpWebRequest later followed by GetRequestStream, but this method is returning an error:

"The requested FTP command is not supported when using HTTP proxy."

I am indeed behind a proxy and required to be.

How can I upload via Powershell when behind a proxy?

This would be solely run from a .ps1 Powershell script.

I have also tried:

$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($server)

$webclient.UploadFile($uri, "C:\Test\1234567.txt")

Where $server and that file are valid. But that code returns this error:

"An exception occurred during a WebClient request."
At C:\Test\script.ps1:101 char:26
+     $webclient.UploadFile <<<< ($uri, "C:\Test\1234567.txt")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I also tried double backslashes in the file paths, didn't help.

The proxy I am under only touches HTTP, and not FTP.

Best Solution

Are you sure your proxy supports FTP, or is it HTTP only? See this thread:

FTP File Upload with HTTP Proxy

For WebClient I've used this in the past although it was for HTTP use, but you could give it a try:

$wc = New-Object System.Net.WebClient
$wc.Headers.Add("User-Agent","Mozilla/4.0+")        
$wc.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$wc.UploadFile($uri, "C:\Test\1234567.txt")

Note that - "The UploadFile method sends a local file to a resource. This method uses the STOR command to upload an FTP resource. For an HTTP resource, the POST method is used."

Related Question