bashpowershellsshwinscpwinscp-net

Running command on freeSSHD server with WinSCP fails with "Your shell is probably incompatible with the application (BASH is recommended)"


I am using below script to Verify checksum of a remote file against a local file. The server I installed on my machine is freeSSHd.

When I tried to execute the below script using PowerShell ISE I get an error message saying:

Your shell is probably incompatible with the application (BASH is recommended)

Screenshot of error

I've granted shell access in the FreeSSHd Server User properties: FreeSSHd settings

Script:

param (
    # Use Generate URL function to obtain a value for -sessionUrl parameter.
    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/",
    [Parameter(Mandatory = $True)]
    $localPath,
    [Parameter(Mandatory = $True)]
    $remotePath,
    [Switch]
    $pause = $False
)

try
{
    Write-Host $localPath -foregroundcolor Gray


# Calculate local file checksum
$localChecksum = ((CertUtil -hashfile $localPath SHA1)[1] -replace " ","")

# Write-Host "Local Checksum:"
Write-Host $localChecksum

# Load WinSCP .NET assembly
#Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")
[Reflection.Assembly]::LoadFrom("\\c:\Program Files (x86)\WinSCP\WinSCPnet.dll") | Out-Null

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl($sessionUrl)

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    Write-Host $remotePath -foregroundcolor Gray

    # Calculate remote file checksum
    $sha1Command = "bash sha1sum -b $remotePath | awk '{print `$1}'"

    $result = $session.ExecuteCommand($sha1Command)
    $result.Check()
    $remoteChecksum = $result.Output;
    #$remoteChecksum =
        [System.BitConverter]::ToString($session.CalculateFileChecksum("sha-1", $remotePath))

    # Write-Host "Remote Checksum:"
    Write-Host $remoteChecksum
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}

# Compare cheksums
if ($localChecksum -eq $remoteChecksum)
{
    Write-Host
    Write-Host "Match" -foregroundcolor "green"
    $result = 0
}
else
{
    Write-Host
    Write-Host "Does NOT match" -foregroundcolor "red"
    $result = 1
}
}
catch [Exception]
{
Write-Host $_.Exception.Message
$result = 1
}

# Pause if -pause switch was used
if ($pause)
{
    Write-Host "Press any key to exit..."
    [System.Console]::ReadKey() | Out-Null
}

exit $result

Solution

  • FreeSSHd server does not support any "bash". Its "shell" is Windows cmd.exe.

    Your code cannot work. Windows cmd.exe is not compatible with WinSCP.

    Moreover FreeSSHd is pretty buggy, do not use it.


    You should use another Windows SSH server.

    There are lot of other options.