Reputation: 1214
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:
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?
Upvotes: 4
Views: 1545
Reputation: 1255
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
.
Upvotes: 3