测试 SSH 会话连接是否失败



我在位于Windows Server 2008 R2中的PowerShell脚本中使用ssh-session模块1.4版本,但是在Windows Server 2019中安装sshsession模块2.1.3后,有些东西不起作用!

我试过if -not

我尝试使用变量-eq

if (! (Get-SshSession -ComputerName $MAST_NAME).connected) {
New-SshSession -ComputerName $MAST_NAME -Username $USR_REMOTE_MAST_NAME -Password $USR_REMOTE_MAST_PASSWD
}

使用 1.4ssh-session模块,测试if (! (get .......)工作正常。 使用 2.1.3sshsession模块(注意到名称已更改(,下面的代码不起作用。

当您创建一个 New-SshSession 时,会创建一个$SshSessions变量。尝试使用它首先查找所需的主机,然后检查其状态。例如:

ForEach ($SshSession in $SshSessions){
If (($SshSession.Host -eq $MAST_NAME) -and ($SshSession.Connected -eq "true")){
Write-Host '$MAST_NAME is connected'
}
Else {
New-SshSession -ComputerName $MAST_NAME -Creditials...}}

最新更新