I downloaded the SSH sessions of Joakim Svendsen , which uses SSH.NET, and installed the PowerShell module on a Windows Jenkins 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 does not crash when the file is not found. Jenkins output:
> 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 also get an error for this, although it is different.
Import-Module Posh-SSH
$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..."
$secPasswd = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($username, $secPasswd)
echo $credentials
$session = New-SSHSession -Computername $devApp1 -Credential $credentials -Acceptkey:$true
$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
Receive error message as:

The same code works on my local laptop, but does not work on the Jenkins server.
, Jenkins - Windows $secpasswd, PSCredential. , POSH .
? ?