我在 Windows PowerShell 的
.ps1
配置文件中创建了此函数:
function sshpc ([int]$port = 8888) {
ssh -fNL [int]$port:localhost:[int]$port pc
}
但是端口转发命令不起作用。它打印消息:
错误的本地转发规范“[Int][Int]8888”
但是运行命令
ssh -fNL 8888:localhost:8888 pc
工作正常。我尝试过的:
我尝试使用参数定义:
($port)
、([int]$port)
、($port = 8888)
,但这些也失败了。function sshpc {
ssh -fNL 8888:localhost:8888 pc
}
[int]$port:localhost:[int]$port
您不能以这种方式使用强制转换,而且它们也不是必需的,因为您只能将 strings 作为参数传递给外部程序(除此之外,
$port
已经是 [int]
类型的 is)。
相反,使用(隐式)可扩展字符串来组成你的论点:
ssh -fNL ${port}:localhost:${port}