PowerShell中变量旁边的冒号(:)的问题

时间:2018-01-25 06:16:34

标签: powershell

我正在尝试让以下内容工作,以便我可以自动完成我需要执行的一些SCP上传。我认为问题在于如何解释${user}@${device}

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

我已经尝试$user@$device(powershell咆哮语法),$user@${device}${user}@${device}(这些告诉我你不能从本地复制到本地,这表明没有解析:/shared/tmp/正确。)

4 个答案:

答案 0 :(得分:1)

您还可以使用:转义`

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

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

试试这个:

$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

答案 3 :(得分:0)

与往常一样,除非您有使用Start-Process的具体原因:不要打扰。请改用call operator&)。

当我测试它时,这完全没问题:

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

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