0

Many methods using PowerShell exist to upload a file in a remote server.

I am using the System.Net.WebClient object to perform that task, and the Start-Process command to do this in background.

My goal is to perform that task using only a one line command, via cmd.exe.

The command below is used :

PowerShell.exe Start-Process -FilePath "PowerShell.exe" -ArgumentList "-c `"$uploadFile='PATH'; $linkFTP='ftp://user:pass@IP/file'; $client=New-Object -TypeName System.Net.WebClient; $URI=New-Object -TypeName System.Uri -ArgumentList $ftp; $client.UploadFile($URI,$uploadFile)`" "

This command ensures that via cmd.exe, you will perform a FTP file upload in background using PowerShell. Using a script file directly instead of using -FilePath "PowerShell.exe" -ArgumentList "-c..." will work, however with that method it will not.

You will notice if you run that command that it is certainly due to quotes problems.

Do you have any clue about it?

Thanks

Duke Nukem
  • 319
  • 4
  • 15
  • If you are having issues with quoting then using `-EncodedCommand` would be a great approach: https://stackoverflow.com/questions/6295957/using-powershell-encodedcommand-to-pass-parameters – Matt Jun 01 '17 at 12:06
  • Nice approach, I will try it. – Duke Nukem Jun 01 '17 at 12:46

1 Answers1

0
Start-Process -FilePath "PowerShell.exe" -ArgumentList "-c {$uploadFile='PATH'; $linkFTP='ftp://user:pass@IP/file'; $client=New-Object -TypeName System.Net.WebClient; $URI=New-Object -TypeName System.Uri -ArgumentList $ftp; $client.UploadFile($URI,$uploadFile)} "

Edit:

Note:$ftp variable is not defined

Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26
  • You use square brackets to try to solve the problem. However, I tried with your solution but an error occured telling that an argument is missing for ArgumentList – Duke Nukem Jun 03 '17 at 20:04
  • your `$ftp` variable is not defined anywhere, so `-ArgumentList` for `New-Object -TypeName System.Uri -ArgumentList` will be null. – Prasoon Karunan V Jun 04 '17 at 05:07