1

I have following script,

$servers = "server1","server2"
ForEach($Server In $servers){
    scp.exe -r 'username@'+$Server+':/home/copylogs/logs' .\logs_prod'
}

but server variable is not working as aspected, any idea?

thanks!

Marc Serra
  • 13
  • 1
  • 3

1 Answers1

0

In order to pass an expression as an argument - a string concatenation operation with + in your case - you must include it in (...)

scp.exe -r ('username@'+$Server+':/home/copylogs/logs') .\logs_prod

Alternatively, you could use an expandable string (string interpolation), "...":

scp.exe -r "username@${Server}:/home/copylogs/logs" .\logs_prod

Note the need to enclose the variable name, Server in {...} in this case, so that the : that follows isn't interpreted as part of the variable name - see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • How do you provide password also with this? I know we should do it with ssh keys but if we want to use password how to do that? – Stack_IQ Dec 16 '20 at 07:34
  • @Stack_IQ Sounds like you need an external utility for that, `sshpass`: see [this answer](https://stackoverflow.com/a/13955428/45375). – mklement0 Dec 16 '20 at 14:43