Search code examples
powershellscp

PowerShell Script SCP command


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!


Solution

  • 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.