Dominic Brunetti
Dominic Brunetti

Reputation: 1069

Issues with colon (:) in when next to variable in PowerShell

I am trying to get the following to work so I can automate some SCP uploads I need to do. I believe the problem is how ${user}@${device} is being interpreted.

$user = "user1"
$device = "server1"
Start-Process 'C:\Program Files\PuTTY\pscp.exe' `
-ArgumentList ("c:\temp\myfile.txt ${user}@${device}:/shared/tmp/") -NoNewWindow

I've tried $user@$device (powershell barks about syntax), $user@${device}and ${user}@${device} (these tell me you can't copy from local to local which indicates in is not parsing the :/shared/tmp/ correctly.)

Upvotes: 1

Views: 1975

Answers (4)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200573

As always, unless you have a specific reason for using Start-Process: don't bother. Use the call operator (&) instead.

This worked perfectly fine when I just tested it:

$user   = 'user1'
$device = 'server1'
$params = 'c:\temp\myfile.txt', "${user}@${device}:/shared/tmp/"

& 'C:\Program Files\PuTTY\pscp.exe' @params

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175084

You can also escape the : with `:

"c:\temp\myfile.txt ${user}@${device}`:/shared/tmp/"

Upvotes: 4

Esperento57
Esperento57

Reputation: 17492

try this:

$user = "user1"
$device = "server1"
$Program='C:\Program Files\PuTTY\pscp.exe'
$Arguments="c:\temp\myfile.txt {0}@{1}:/shared/tmp/ -NoNewWindow" -f $user, $device
Start-Process $Program  $Arguments

Upvotes: 1

Owain Esau
Owain Esau

Reputation: 1922

Try this:

$user = "user1"
$device = "server1"
Start-Process 'C:\Program Files\PuTTY\pscp.exe' `
-ArgumentList ("c:\temp\myfile.txt {$($user)}@{$($device)}:/shared/tmp/") -NoNewWindow

Upvotes: 1

Related Questions