4

I have downloaded SSH-Sessions by Joakim Svendsen which uses SSH.NET and installed the PowerShell module in the Jenkins Windows server

In Jenkins, I have the following PowerShell script:

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;
}

The shell script contains:

#!/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

The problem is that the Jenkins job doesn't get failed when the file is not found. The Jenkins output is:

> Deployment started in 10.0.0.1...... Successfully connected to
> 10.0.0.01
> 10.0.0.01 had an error:

Finished: SUCCESS

After some suggestions I wrote the following PowerShell script using Posh-SSH. I'm also getting an error for this one, though it's different.

#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

Getting the error message as:

Posh-SSH script result

The same code works in my local laptop, but fails in the Jenkins server.

I think in Jenkins server, due to Windows security restrictions, will not store the $secpasswd that is retrieved from the PSCredential. This causes only the username to be supplied to POSH.

How can I either fix those issues? How should I hardcode the password?

shoover
  • 3,071
  • 1
  • 29
  • 40
user2439278
  • 1,222
  • 7
  • 41
  • 75
  • My assumption would be that nothing is happening because you're not passing on that error code from the PowerShell environment to Jenkins. As it is the PowerShell script exists with 0 unless there is an error while importing/establishing the session or Invoking the command. You would need to evaluate and return the code from `Invoke-SSHCommand`. – Seth Apr 28 '17 at 07:34
  • I'm new to powershell.. How to evaluate the Invoke-SSHCommand output – user2439278 Apr 28 '17 at 07:39
  • That would depend on how it's implemented. Look into the documentation for those cmdlets or play around with it. A good start would be to save what's returned to a variable. So `$return = Invoke-SSHCommand ...`. After that you could use [`Get-Member`](https://technet.microsoft.com/en-us/library/ee176854.aspx) to get more information or just let it print to the screen and have a look. Mind you that something like [`$?`](http://stackoverflow.com/questions/10634115/what-is-in-powershell) might be insufficient as the cmdlet itself might actually work fine, though the commands within error. – Seth Apr 28 '17 at 07:51
  • I'm clueless how to use Get-Member to evaluate the InvokeSSHCommand – user2439278 Apr 28 '17 at 09:32
  • The link actually contains an example for that. Shouldn't be that hard and you could also use it on the variable. You should do this on an interactive PowerShell and not in the script. Figure out what the invoke returns and adjust your script afterwards. As an example the default `Invoke-Command` cmdlet. You could also try to run `Invoke-SSHCommand -?` or `Get-Help -Full Invoke-SSHCommand` or use `-Examples` to get more information about how the cmdlet is supposed to work. – Seth Apr 28 '17 at 09:46
  • I tried with Get-Member, but unable to fix the issue. Is there any other way to fix it. – user2439278 May 04 '17 at 05:41
  • What did you try? What additional information did you discover? What were the properties and was there information about the return value of the cmdlet? – Seth May 04 '17 at 05:56
  • i have updated the question by providing Get-Member output and the powershell not dislpaying the error message given from shell script inside the linux box. It displays the success message but not the failure message – user2439278 May 05 '17 at 09:17
  • Did you even try to understand what `Get-Member` do? With your call of `echo $return | Get-Member`, you're telling PowerShell to transform `$return` into a string and afterwards you're checking out the attributes .. which of course are the attributes every string attribute has. – Seth May 05 '17 at 09:28
  • No, you're not. The original link for the documentation has quite a bit of information. If you're also new to programming as a whole it could be different. Sorry though if my words were a bit harsh. If you're not actually using Posh-SSH you might be using [this](http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library#Invoke-SshCommand) (you never said what script you're using). In which case the command seems to only return a string. The link contains an example on how to get output and exit status. – Seth May 05 '17 at 10:07
  • I have downloaded http://www.powershelladmin.com/w/images/6/60/SSH-Sessions.zip and installed powershell module in the Jenkins windows server – user2439278 May 05 '17 at 10:17
  • I used Posh-SSH module now and updated the question with the error message – user2439278 May 05 '17 at 14:18
  • The credentials shouldn't be an issue. What's most likely happening is that Posh-SSH isn't importing successfully. Did you use the Windows built in zip viewer to extract the files? Check that they aren't blocked. You can do this by checking the file properties and unblocking the files. As there is a `Unblock-File` cmdlet you could use it to unblock all files in a directory. See also [Easily Unblock All Files in a Directory Using PowerShell by Hey, Scripting Guy!](https://blogs.technet.microsoft.com/heyscriptingguy/2012/09/14/easily-unblock-all-files-in-a-directory-using-powershell/) – Seth May 05 '17 at 17:01
  • Unblock-File works in PowerShell 3.0 ... I'm using powershell 2.0 version. I also checked the file properties, its not blocked – user2439278 May 05 '17 at 17:33

1 Answers1

3

As you've never stated where those commands are coming from, I'm going to assume you're using Posh-SSH. By looking at this article about it, the solution would probably be:

$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;
}

The if could be left out, it's just there for demonstration purposes. After all the exit status would be 0 otherwise.

The correct way to use Get-Member to get information about $result (in case this does not work) would be: $result | Get-Member. That will output the general attributes of whatever kind of object $result is.

As it would appear you're running this. You would need to run the following to get the objects:

$result = $SshSessions."$dev_app1".RunCommand('cd /NFS_DATA/autodeploy_scripts && echo test@123 | ./autodeploy.sh')

You'd use this instead of the Invoke-SshCommand and would need to change $result.Output to $result.Result.

Seth
  • 1,215
  • 15
  • 35
  • I have used `$result | Get-Member` , it displays the general attribute. How to proceed further – user2439278 May 05 '17 at 09:56
  • Either read the documentation on what those attributes mean or think about it. An attribute named `Output` probably will contain the output of the command executed by `Invoke-SshCommand`. So with your example script I'd expect the attribute to contain a text saying `... not found`. If it does have an attribute `ExitStatus` check whenever it's 0 or 1 and possibly force the error to check whenever it changes to 1. After that use that information to let the script indicate an error (possibly by exiting with a non zero exit code). – Seth May 05 '17 at 10:01
  • I have downloaded powershelladmin.com/w/images/6/60/SSH-Sessions.zip and installed powershell module in the Jenkins windows server – user2439278 May 05 '17 at 11:04
  • I used Posh-SSH module now and updated the question with the error message – user2439278 May 05 '17 at 14:05
  • `$result = $SshSessions."$dev_app1".RunCommand('cd /NFS_DATA/autodeploy_scripts && echo test@123 | ./autodeploy.sh')` and `Write-Output $result.Result` works fine. How to fetch the exit code – user2439278 May 05 '17 at 18:29
  • Does `$result.ExitStatus` have a valid value? If you look at the [documentation](http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library#Invoke-SshCommand_Example) that should contain the `ExitStatus` of the script. If it does you might have to change your termination. But you would have a stepping stone. You could also try to check the value by using `Write-Output "'$($result.Output)'"`. If it ends up being `''` you might have a null value. [This](http://stackoverflow.com/questions/33358242/build-step-windows-powershell-marked-build-as-failure-why) might be helpful? – Seth May 05 '17 at 18:46
  • It Works now. `Write-Output $result.ExitStatus` returns the exit code given in shell script – user2439278 May 05 '17 at 18:58