0

I am trying to use PowerShell to send a file from a Windows 10 dev box to a CentOS 7 server across a local area network.

When I run the following command from PowerShell in the Windows 10 dev box, Putty opens, but then closes before asking for password. And then the test file (index.html) is not present in the intended destination folder on the CentOS 7 server.

Start-Process 'C:\Program Files (x86)\PuTTY\pscp.exe' -ArgumentList ("-scp -pw password C:\projects\temp\junk\index.html some_user_name@192.168.1.5:/home/some_user_name/")  

How can I diagnose what problem is causing this command to fail? And what specific steps need to be taken in order for the file to be successfully transferred from the devbox to the server?

I have double checked the obvious things, like the file paths, the user name, and the IP.

The above command is derived from this suggestion by @GrahamGold.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • pscp.exe being the gui client? ;) offtopic, why do you have the args in ()? – Jaqueline Vanek Jun 07 '17 at 18:43
  • @JaquelineVanek [it seems](https://superuser.com/questions/532919/how-to-use-pscp-to-copy-file-from-unix-machine-to-windows-machine-where-target-p) pscp works in command line. I'm also wondering about the parens :). Edit: the link I provided explains you could also use the .NET assembly for WinSCP. – sodawillow Jun 07 '17 at 18:44
  • You don't need any of the boilerplate Start-Process stuff; `pscp -scp -pw pass c:\folder\file.txt username@servername:~/` transfers fine for me, from the powershell prompt... does your password have $ symbols in it? You use double quotes, so you need to escape PowerShell special characters inside it.. – TessellatingHeckler Jun 07 '17 at 18:49
  • @sodawillow Please see link to source of the code, which I just added to the end of the OP. – CodeMed Jun 07 '17 at 18:54
  • "and you use the -pw switch in pscp to pass in a password" so, why do you expect putty asking for a pass? – Jaqueline Vanek Jun 07 '17 at 18:56
  • Any reason you use `Start-Process`? Why don't you use simply `& "C:\Program Files (x86)\PuTTY\pscp.exe" -scp ...`? - Did you test the same command on command-line? What does it do? `"C:\Program Files (x86)\PuTTY\pscp.exe" -scp -pw password C:\projects\temp\junk\index.html some_user_name@192.168.1.5:/home/some_user_name/"` – Martin Prikryl Jun 07 '17 at 18:58
  • why even powershell for that matter? – Jaqueline Vanek Jun 07 '17 at 18:59
  • @TessellatingHeckler Your answer works. If you want to write it up as an answer, I would be happy to mark it as accepted and +1. I think that the community would greatly appreciate if you invested a little time explaining the concepts behind how it works and why. – CodeMed Jun 07 '17 at 19:01
  • guess we can remove the powershell tag now? ;) – Jaqueline Vanek Jun 07 '17 at 19:03
  • @CodeMed It actually does not make sense, that it *just works* by removing the `Start-Process`, leaving other things the same - Your code is correct as such. – Martin Prikryl Jun 07 '17 at 19:09
  • @JaquelineVanek Unless you can show how to make it work with PowerShell. See the link at the end of the OP to the source of this approach. – CodeMed Jun 07 '17 at 19:11
  • @MartinPrikryl The code that works on my devbox is `pscp -scp -pw pass C:\projects\temp\junk\index.html some_user_name@192.168.1.5:~/`. Does this still not make sense? – CodeMed Jun 07 '17 at 19:13
  • @CodeMed I don't know what the concept is, I just wrote the command without the fluff and it worked. I don't know why GrahamGold wrapped it in Start-Process or why Start-Process didn't work for you, or how your version of pscp launched a GUI - do you use the Putty Agent in your systray? – TessellatingHeckler Jun 07 '17 at 19:13
  • @CodeMed Maybe you have 64-bit version of PuTTY/pscp installed? So the path is `C:\Program Files` and not `C:\Program Files (x86)`? - Or the `/home/some_user_name/` path is wrong. – Martin Prikryl Jun 07 '17 at 19:15
  • As I've suggested you above, try `"C:\Program Files (x86)\PuTTY\pscp.exe" -scp -pw password C:\projects\temp\junk\index.html some_user_name@192.168.1.5:/home/some_user_name/` on `cmd.exe` console, and you will see what is wrong. – Martin Prikryl Jun 07 '17 at 19:17

1 Answers1

0

In general, there's nothing wrong with your command. It should work (if all arguments are correct).


To diagnose the problem, first test the command on Windows command-line (cmd.exe):

"C:\Program Files (x86)\PuTTY\pscp.exe" -scp -pw password C:\projects\temp\junk\index.html some_user_name@192.168.1.5:/home/some_user_name/

This way, the output is preserved and you will see, what went wrong (if anything).

The next step is to do the same in PowerShell script using call operator & (though the result should be the same):

& "C:\Program Files (x86)\PuTTY\pscp.exe" -scp -pw password C:\projects\temp\junk\index.html some_user_name@192.168.1.5:/home/some_user_name/

If that works, it's time to replace & with the Start-Process cmdlet, if you really want that. But in general, I do not see a point of that, unless you want to run the transfer asynchronously.


Though if you want to make you command portable, you need to add -hostkey switch.

See my answer to Using echo y as an automated response to a pcp hostkey prompt.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992