1

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");
IcyBrk
  • 1,090
  • 12
  • 21
  • Show us your C# code that works. – Martin Prikryl Apr 12 '19 at 06:29
  • Martin, I am thankful to your help, your answer is very relevant. I posted the C# code in my original post. – IcyBrk Apr 16 '19 at 18:42
  • There's no protocol specified in your C# code. Your code will not upload the file anywhere. It will copy it to a local folder `waws-someaddress.ftp.azurewebsites.windows.net\site\wwwroot\PictureReview` (under your programs startup folder) -- Add `ftp://` to the URL --- Then your C# code will probably fail with the same error --- If it does, show us its [network log](https://stackoverflow.com/q/9664650/850848). – Martin Prikryl Apr 17 '19 at 05:08
  • What does your edit mean? --- And note that your C# and PowerShell code uses a completely different URL -- Why? What does that mean? – Martin Prikryl Apr 17 '19 at 20:05
  • You are right, without "ftp://" protocol, that code is not working, when I edit that post, I must forget to put it in. Actually I just tried that code with ftp protocol in url parameter, it works fine. I just reedit that part to clarify. – IcyBrk Apr 17 '19 at 20:14
  • You still didn't explain the the difference between URLs in PowerShell and C# code. – Martin Prikryl Apr 17 '19 at 20:59
  • Martin, sorry, that is my mistake, they should be same, I edited my that url. – IcyBrk Apr 19 '19 at 19:33
  • What about the credentials? They do not match either. Do you really have `$` sign in the username? Are you aware that you need to escape `$` sign in PowerShell string? You should really show us some *convincing* examples, that prove that your C# and PowerShell codes match. – Martin Prikryl Apr 22 '19 at 18:47
  • Martin, thank you for persisting following up, when I add ` to escape "$" in the username variable, it works, your comments solved my question. – IcyBrk Apr 23 '19 at 22:31

1 Answers1

1

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'
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992