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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 Sounds like you need an external utility for that, sshpass: see this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.