在 Windows PowerShell 中设置 SSH 命令的别名

chr*_*ton 6 ssh powershell alias command-line windows-10

在 Windows PowerShell 中:

>ssh -i \Path\To\key.pem user@server.com像冠军一样工作。

>Set-alias sshalias "ssh -i \Path\To\key.pem user@server.com"保存别名时没有错误。

>sshalias 返回以下错误:

sshalias : The module 'ssh -i ' could not be loaded. For more information, run 'Import-Module ssh -i '.

At line:1 char:1 + sshalias

+ CategoryInfo : ObjectNotFound: (ssh -i \Path\To\Key.pem user@server.com:String) [], CommandNotFoundException

`+ FullyQualifiedErrorId : CouldNotAutoLoadModule`
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

Jef*_*lin 5

PowerShell 不允许别名包含命令参数。相反,要将命令的特定参数包装到“别名”中,请使用函数:

function sshalias {
    ssh -i \Path\To\key.pem user@server.com
}
Run Code Online (Sandbox Code Playgroud)

您可能需要引用或转义某些参数;我不使用 ssh,并且与其他旧命令的结果不一致。


Bil*_*art 4

您正在尝试将整个字符串作为命令执行。命令是ssh,后面有参数。(也就是说,您的系统上没有名为“ ssh -i \Path\To\Key.pem "user@server.com"”的可执行文件或脚本。)

解决办法是正确执行命令:

ssh -i \Path\To\Key.pem "user@server.com"
Run Code Online (Sandbox Code Playgroud)

但根据定义,PowerShell 别名不能有任何参数。

解决方法是使用函数代替:

function sshalias { ssh -i \Path\To\Key.pem "user@server.com" }
Run Code Online (Sandbox Code Playgroud)