[英]how to validate New-ssh Session in Powershell
我已经下载了Joakim Svendsen的SSH-Sessions,它使用SSH.NET,并在Jenkins Windows服务器中安装了PowerShell模块。
在Jenkins中,我具有以下PowerShell脚本:
Import-Module SSH-Sessions
$lastExitCode = 0
$devApp1 = "10.0.1.109"
$devApp2 = "10.0.1.110"
Write-Output "Deployment started in $devApp1......"
New-SshSession -ComputerName $devApp1 -Username test -Password test@123
问题是当连接失败时,詹金斯作业不会失败。 Jenkins的输出为:
Unable to connect to 10.0.1.109: Exception calling "Connect" with "0" argumen t(s): "No connection could be made because the target machine actively refused it" Finished: SUCCESS
如何解决此问题?
使用本文档 ,我将执行以下操作:
New-SshSession -ComputerName $devApp1 -Username test -Password test@123
if (!$SshSessions.$devApp1.Connected) {
throw "Session to $devApp1 is not connected"
}
或这个:
New-SshSession -ComputerName $devApp1 -Username test -Password test@123
$Session = Get-SshSession -ComputerName $devApp1
if (!$Session.Connected) {
throw "Session to $devApp1 is not connected"
}
显然,如果您有多个与$devApp1
连接, $devApp1
此代码将不起作用,但是文档中的示例表明,它不允许您这样做。 出于任何原因, New-SshSession
不支持-PassThru
参数,它似乎也不返回会话,也不允许您为该会话指定变量。 相反,设计使用全局变量: $SshSessions
。 不是我设计的方式。
您可能需要研究Posh-SSH 。 我不记得这样做是否更好,但我不记得这种问题。 它还使用SSH.Net库。 或者, WinSCP具有.Net程序集 ,可从PowerShell中轻松使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.