shell - Powershell脚本在Jenkins中没有失败

标签 shell powershell jenkins invoke-command

我已经下载了SSH-Sessions by Joakim Svendsen它使用 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 安全限制,不会存储 $secpasswd从 PSCredential 中检索到的。这会导致仅将用户名提供给 POSH。

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

最佳答案

由于您从未说明这些命令的来源,我假设您使用的是 Posh-SSH .通过查看 this article关于它,解决方案可能是:

$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是。

看起来你是 running this .您需要运行以下命令来获取对象:
$result = $SshSessions."$dev_app1".RunCommand('cd /NFS_DATA/autodeploy_scripts && echo test@123 | ./autodeploy.sh')

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

关于shell - Powershell脚本在Jenkins中没有失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43663159/

相关文章:

Azure 函数 | PowerShell |无法加载类型 'System.Security.Cryptography.SHA256Cng'

jenkins - 从 Jenkinsfile 中的 xml 文件获取属性值

jenkins - 如果环境变量包含特定子字符串,则执行一个阶段

linux - 如何使用 linux 命令从另一个文件创建一个只包含小写字母的新文件?

shell - Groovy Init 中的 Jenkins 环境变量

linux - 我可以在 shell 脚本中使用 hexdump 吗?

jenkins - 即使上一个作业在 Jenkins 构建流程中失败,也按顺序运行下一个作业

arrays - 无法在 Bash Shell 脚本中打印整个数组

powershell - 如何使用多个参数仅在PowerShell中强制高级功能的位置0

json - 在 PowerShell 中迭代 JSON 并删除 JSON 元素