0

I am trying to write a Powershell script that copies files from a Windows PC to a Linux box.

I am able to SSH into my remote Linux machine from a Powershell window using SSH keys (stored in %userprofile%/.ssh/ on the Windows machine. I can also run an SCP command from the terminal window copying files from the Windows machine to the Linux box.

However, the very same scp command fails when run from a script (script.ps1) with the following error:

ssh: Could not resolve hostname <hostname>: No such host is known.
lost connection

Anyone know why scp cannot resolve the hostname when run as a script in Powershell, when it can from the terminal?

The scp command I am running looks like:

& C:\Windows\System32\OpenSSH\scp.exe -r 'bin/release/net7.0/linux-x64/publish' <user_name>@<comp_name>:/home/<user_name>/containers/net_ws/app

As an aside, I tried to ping the <comp_name> from within the same script and it pings successfully, so there appears to be no issue resolving the comp_name host within the powershell context.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Stephen Ellis
  • 2,561
  • 2
  • 24
  • 47
  • There's not enough information in your question to diagnose the problem. I wouldn't expect it to make a difference, but to ensure that your target path isn't subject to potentially unwanted interpretation, enclose it in `'...'` too. If you need _string interpolation_, use `"..."` – mklement0 Feb 13 '23 at 23:16
  • The hostname is being resolved on the local machine and not the remote machine. When you type the command in a terminal it is being executed on the remote machine. You are passing a string command to the remote machine. Put entire arguments in a variable surrounded by single quotes so there is no substitution in PS. Also use two sets of single quotes around the single quotes. : $arguments = '-r ''bin/release/net7.0/linux-x64/publish'' @:/home//containers/net_ws/app' then : & C:\Windows\System32\OpenSSH\scp.exe $arguments. – jdweng Feb 13 '23 at 23:21
  • I've tried a number of different combinations of " and 's as I was wondering if I was running into string interpolation issues. @mklement0, can you suggest further information I can provide? – Stephen Ellis Feb 13 '23 at 23:40
  • @jdweng, you can _not_ use a _single string_ to encode _multiple_ arguments with direct invocation (possibly via `&`) - see [this answer](https://stackoverflow.com/a/73208537/45375). Please consider deleting your comment. – mklement0 Feb 13 '23 at 23:40
  • 1
    FYI, I've managed to make the script work by renaming the `` to it's IP address. In this case, it's something specifically around how `scp` is trying to resolve the IP address. – Stephen Ellis Feb 13 '23 at 23:46
  • @mklement0 : You are not send arguments. You are sending a command string to a remote device. The -r should not of been part of the arguments since it doesn't get sent to the remove device. – jdweng Feb 14 '23 at 09:38
  • 1
    @jdweng, multiple arguments must be passed to `sdp.exe`, _locally_, as shown in the question. They cannot be passed _as a single string_ as suggested in your first comment (maybe your'e thinking of `ssh`, not `scp`). To pass multiple arguments _programmatically_ to an external program, use an _array_, as discussed in the previous answer I mentioned. Please consider deleting your comments, as they are irrelevant to the question and suggest an approach that cannot work. – mklement0 Feb 14 '23 at 13:31

1 Answers1

0

Problem solved, thought full explanation is lacking.

I have solved the script issue by replacing the comp_name with the ip address of the device.

& C:\Windows\System32\OpenSSH\scp.exe -r 'bin/release/net7.0/linux-x64/publish' 
<user_name>@<ip_address>:/home/<user_name>/containers/net_ws/app

I'm still curious as to why scp is unable to resolve the ip address from the host name in this instance - i.e. when running as part of a script, rather than in the terminal, especially since other executables running in the same script context are able to find the host.

Stephen Ellis
  • 2,561
  • 2
  • 24
  • 47