在Powershell中,运行“ Invoke-SSHCommand”时会抛出异常,因为找不到参数“ SSHSession”

问题描述

我尝试如下

$session = New-SSHSession -ComputerName $IPAddress -Credential $creds -AcceptKey
$result = Invoke-SSHCommand -SSHSession $session -Command "hostname"

例外情况如下

Invoke-SshCommand : A parameter cannot be found that matches parameter name 'SSHSession'.
+ $result = Invoke-SSHCommand -SSHSession $session -Command $updatepack ...
+ CategoryInfo          : InvalidArgument: (:) [Invoke-SshCommand],ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Invoke-SshCommand

我检查了命令的文档,如下所示

语法

Invoke-SshCommand [[-ComputerName] <String[]>] [-Command] <String> [-Quiet] [-InvokeOnAll] [<CommonParameters>]

Invoke-SshCommand [[-ComputerName] <String[]>] [-ScriptBlock] <ScriptBlock> [-Quiet] [-InvokeOnAll] [<CommonParameters>]

我看过一些示例,这些示例的参数名称 SSHSession ,不确定我导入的模块版本是否错误

解决方法

我假设您已经拥有Posh-SSH模块。 (由于未收到错误消息:未被识别为cmdlet的名称

我建议您检查POSH-SSH的版本

Get-module -Name Posh-SSH

Get-command Invoke-SSHCommand 

enter image description here

检查Posh-SSH版本的版本。

如果版本很旧,请尝试更新到最新版本。

Install-Module -Name Posh-SSH -RequiredVersion 2.1
,

“ Invoke-SSHCommand”是Powershell模块中名为“ PoshSSH”的cmdlet。如果要使用它,则需要先安装它。

请检查this link以下载/安装它。

谢谢。