0

I am using PowerShell Scripts to upload the files by FTP. The scripts I use is following:

$File = "C:\test\test01.txt"
$ftp = "ftp://MyUsername:MyPassword@MyHostName/"

"ftp url: $ftp"

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

"Uploading $File..."

$webclient.UploadFile($uri,$File)

But When I run the scripts, I get the following errors:

 Exception calling "UploadFile" with "2" argument(s): "The requested URI is invalid for this FTP command."
At C:\FTP.ps1:11 char:1
+ $webclient.UploadFile($uri,$File)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

The Powershell Version I use is following:

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1

Anyone knows how to solve this problem?

Thanks

Terry Zhang
  • 4,541
  • 8
  • 23
  • 29

1 Answers1

0

A possible explenation could be that your username/password contains special characters that are not allowed in a valid URI (such as @ or :).

Instead you should set your credentials using System.Net.NetworkCredential

See this answer for reference

Community
  • 1
  • 1
Daniel
  • 10,641
  • 12
  • 47
  • 85