0

我正在尝试自动化一个过程,其中我有一个 powershell 脚本 SSH 到我们的 cisco 交换机并打开和关闭 Vlan 接口。当我尝试运行它时,我得到“使用“1”参数调用“EndExecute”的异常:“已建立的连接被服务器中止。””

我的脚本中缺少什么导致它像这样出错?谢谢。

Install-Module -Name Posh-SSH

#Set Creds
[ValidateNotNullOrEmpty()]$secpasswd = "myPass"
$secpasswd = ConvertTo-SecureString -String $secpasswd -AsPlainText -Force
$mycreds = New-Object Management.Automation.PSCredential ("myUser", $secpasswd)

#Build SSH session
New-SSHSession "MyIP" -Port 22 -Credential $mycreds
#Assign current session to Var.
$s = Get-SSHSession | Select -ExpandProperty SessionID -First 1

#Send SSH Commands
Invoke-SSHCommand -Command "show log" -SessionId $s
Invoke-SSHCommand -Command "conf t" -SessionId $s
Invoke-SSHCommand -Command "interface vlan 310" -SessionId $s
Invoke-SSHCommand -Command "shut" -SessionID $s
Invoke-SSHCommand -Command "no shut" -SessionID $s
Invoke-SSHCommand -Command "exit" -SessionId $s
Invoke-SSHCommand -Command "exit" -SessionId $s

#Close SSH-Session
Remove-SSHSession -SessionId $s
4

1 回答 1

0

尝试使用 SSHShellStream > SSHStreamShellCommand

$SSHsession=New-SSHSession -ComputerName $YourSwitchIP -Credential $Cred -AcceptKey 
$SSHStream = New-SSHShellStream -Index $SSHsession.SessionId
#---> Enter conf
$cmd="conf"
Invoke-SSHStreamShellCommand -ShellStream $SSHStream -Command $cmd
于 2022-01-05T15:11:39.090 回答