Powershell脚本在Jenkins中未能失败

4

我已经下载了Joakim Svendsen的SSH-Sessions,它使用了SSH.NET并在Jenkins Windows服务器中安装了PowerShell模块。

在Jenkins中,我有以下的PowerShell脚本:

Import-Module SSH-Sessions

$lastExitCode = 0
$devApp1 = "10.0.0.1"
$devApp2 = "10.0.0.2"

Write-Output "Deployment started in $devApp1......"

New-SshSession -ComputerName $devApp1 -Username test -Password test@123
$return = Invoke-SshCommand -ComputerName $devApp1  -Command "cd /NFS_DATA/autodeploy_scripts && echo test@123 | ./autodeploy.sh"

$return | Get-Member

if ($lastExitCode -ne 0)
{
    Write-Output $lastExitCode
    exit 1;
}
else
{
    Write-Output $lastExitCode
    exit 0;
}

这个 shell 脚本包含:

#!/bin/bash
file="/NFS_DATA/autodeploy_scripts/test.log"
if [ -f "$file" ]
then
        echo "$file found."
        exit 0;
else
        echo "$file not found."
        exit 1;
fi

问题在于当文件未找到时,Jenkins作业不会失败。Jenkins的输出如下:
> Deployment started in 10.0.0.1...... Successfully connected to
> 10.0.0.01
> 10.0.0.01 had an error:

Finished: SUCCESS

在一些建议之后,我使用Posh-SSH编写了以下PowerShell脚本。但是,我也遇到了一个错误,尽管它与之前的不同。

#Import-Module SSH-Sessions
Import-Module Posh-SSH

# Setup static variables
$devApp1="10.0.0.1"
$devApp2="10.0.0.2"
$username = "test"
$password = "test@123"
$command = "cd /NFS_DATA/autodeploy_scripts && echo Hybris@123 | ./autodeploy.sh"

Write-Output "Deployment started in $devApp1..."

# Setup PSCredentials
$secPasswd   = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($username, $secPasswd) 
echo $credentials

# Estalbish new SSH session automatically accepting new SSH keys
$session = New-SSHSession -Computername $devApp1 -Credential $credentials -Acceptkey:$true

# Invoke command to be run on/in the SSH session
$output = Invoke-SSHCommand -SSHSession $session -Command $command

Write-Output "Returned Output from the Command: "
Write-Output $output.Output
Write-Output "Last Exit Status: "
Write-Output $output.ExitStatus

获取错误信息如下:

Posh-SSH script result

相同的代码在我的本地笔记本电脑上可以运行,但在Jenkins服务器上失败了。

我认为在Jenkins服务器上,由于Windows安全限制,不会存储从PSCredential检索到的$secpasswd。这导致只提供用户名给POSH。

我该如何解决这些问题?我应该如何硬编码密码?


我尝试使用Get-Member,但无法解决问题。还有其他方法可以解决吗? - user2439278
我已经更新了问题,提供了Get-Member输出,并且PowerShell没有显示来自Linux盒子内部的shell脚本给出的错误消息。它显示成功消息,但不是失败消息。 - user2439278
不,你不是。文档的原始链接包含了相当多的信息。如果你还是一个新手程序员,那可能会有所不同。如果我的话有点过于苛刻,对不起。如果你实际上没有使用 Posh-SSH,那么你可能正在使用这个(你没有说你正在使用哪个脚本)。在这种情况下,该命令似乎只返回一个字符串。链接中包含了一个示例,说明如何获取输出和退出状态。 - Seth
我现在使用了 Posh-SSH 模块,并更新了问题并附上了错误信息。 - user2439278
凭证不应该是问题。最有可能的情况是Posh-SSH没有成功导入。您是否使用Windows内置的zip查看器提取文件?检查它们是否被阻止。您可以通过检查文件属性并取消阻止文件来解决此问题。由于存在“Unblock-File” cmdlet,您可以使用它来取消目录中所有文件的阻止。另请参见Easily Unblock All Files in a Directory Using PowerShell by Hey, Scripting Guy! - Seth
显示剩余9条评论
1个回答

3

由于您没有说明这些命令来自哪里,我将假设您正在使用Posh-SSH。通过查看此文章,解决方案可能是:

$dev_app1="10.00.00.01"
$dev_app2="10.00.00.02"

echo "Deployment started in $dev_app1......"
Import-Module SSH-Sessions
New-SshSession -ComputerName $dev_app1 -Username test -Password test@123
$result = Invoke-SshCommand -ComputerName $dev_app1  -Command "cd /NFS_DATA/autodeploy_scripts && echo test@123 | ./autodeploy.sh"

Write-Output "Returned Output from the Command: "
Write-Output $result.Output
Write-Output "Last Exit Status: "
Write-Output $result.ExitStatus

if($result.ExitStatus -ne 0){
    exit $result.ExitStatus;
}
if可以省略,它只是为了演示目的而存在。毕竟退出状态否则将为0。
正确使用Get-Member获取有关$result信息的方法(如果不起作用)是:$result | Get-Member。这将输出$result是什么类型对象的一般属性。
由于您似乎正在运行此操作。您需要运行以下操作才能获取对象:
$result = $SshSessions."$dev_app1".RunCommand('cd /NFS_DATA/autodeploy_scripts && echo test@123 | ./autodeploy.sh')

您需要使用这个而不是Invoke-SshCommand,并且需要将$result.Output更改为$result.Result


我已经使用了 $result | Get-Member,它显示了一般属性。接下来该怎么做? - user2439278
我已经下载了powershelladmin.com/w/images/6/60/SSH-Sessions.zip,并在Jenkins Windows服务器上安装了PowerShell模块。 - user2439278
$result = $SshSessions."$dev_app1".RunCommand('cd /NFS_DATA/autodeploy_scripts && echo test@123 | ./autodeploy.sh')Write-Output $result.Result 运行正常。如何获取退出码? - user2439278
$result.ExitStatus 是否有有效值?如果查看文档,它应该包含脚本的 ExitStatus。 如果有,您可能需要更改终止方式。但这是一个垫脚石。 您还可以尝试使用 Write-Output "'$($result.Output)'" 检查该值。 如果最终是 '',则可能为 null 值。 这个 链接 可能会有帮助? - Seth
现在它可以工作了。Write-Output $result.ExitStatus 返回 shell 脚本中给出的退出代码。 - user2439278
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接