IcyBrk
IcyBrk

Reputation: 1270

Error 501 when connecting to Azure FTP with PowerShell but works in C#

I tried to use WebClient in PowerShell 5.1 to upload or download some file to Azure AppService hosted website. I got the FTP credential and FTP URL from my app's PublishProfile in Azure and used code below to do the task,

$username = "$somename"
$password = "somepassword"
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = New-Object 
System.Net.NetworkCredential($username, $password)

$localfilename = "Temp.txt"

$destUrl = "ftp://waws-someaddress.ftp.azurewebsites.windows.net/site/wwwroot/PictureReview/temp.txt"

$uri = New-Object System.Uri($destUrl)

#Write-Host $uri

$webclient.UploadFile($uri, $localfilename)
$webClient.Dispose()

However as I tried, whenever using UploadFile() or DownloadFile(), it always gives me

Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (501) Syntax error in parameters or arguments." The remote server returned an error: (501) Syntax error in parameters or arguments.

I tried another public testing FTP server URL, like ftp.dlptest.com instead of Azure FTP URL, and it works very well. Does anyone ever meet this problem? Is this an issue of Azure App Service?

I also tried the same approach in C# code with Azure FTP URL, it also works well.

By the request of the poster, the C# code is like below:

WebClient client = new WebClient(); 
client.Credentials = new NetworkCredential("$somename", "somepassword"); 
client.UploadFile("ftp://waws-someaddress.ftp.azurewebsites.windows.net/site/wwwroot/PictureReview/temp.txt", @"C:\users\blahblah\Desktop\temp.txt");

Upvotes: 1

Views: 663

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202282

Your C# code shows that you have $ sign in your username.

$ sign has special meaning in a double-quoted PowerShell string. To use a literal $ sign, you need to escape it with a back tick:

$username = "`$somename"

Or use single quotes:

$username = '$somename'

Upvotes: 1

Related Questions