我正在使用下面的脚本来针对本地文件验证远程文件的校验和。我在计算机上安装的服务器是freeSSHd。

当我尝试使用PowerShell ISE执行以下脚本时,出现一条错误消息:



bash - 使用WinSCP在freeSSHD服务器上运行命令失败并显示 “Your shell is probably incompatible with the application (BASH is recommended)”-LMLPHP

我已经在FreeSSHd Server用户属性中授予了Shell访问权限:
bash - 使用WinSCP在freeSSHD服务器上运行命令失败并显示 “Your shell is probably incompatible with the application (BASH is recommended)”-LMLPHP

脚本:

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

最佳答案

FreeSSHd服务器不支持任何“bash”。它的“ shell ”是Windows cmd.exe

您的代码无法正常工作。 Windows cmd.exe与WinSCP不兼容。

此外,FreeSSHd相当容易出错,请不要使用它。

您应该使用其他Windows SSH服务器。

  • 您可以使用Windows build of OpenSSH。它将允许您在服务器上执行PowerShell脚本以计算校验和。

    如果安装Windows Subsystem for Linux,甚至可以获取sha1sum(但我不确定)。
  • 如果需要在Windows上模拟* nix环境,则可以使用Cygwin
  • 您可以免费使用Bitvise SSH Server个人使用。它的SFTP服务器自己支持校验和计算,因此您可以直接使用WinSCP Session.CalculateFileChecksum method

  • 还有很多其他选择。

    07-24 09:39
    查看更多