tomohulk / WinSCP

WinSCP PowerShell Wrapper Module
GNU General Public License v3.0
153 stars 30 forks source link

WinSCP Version is newer than Module #160

Closed rik-e closed 4 months ago

rik-e commented 5 months ago

Issue Description

Write-Error: Exception calling "Open" with "1" argument(s): "The version of C:\Program Files (x86)\WinSCP\winscp.exe (6. 3.3.0) does not match version of this assembly C:\Program Files\WindowsPowerShell\Modules\WinSCP\6.3.2.0\lib\net40\WinSC Pnet.dll (6.3.2.0)." (NotSpecified: (:) [Write-Error], WriteErrorException) Exception calling "Open" with "1" argument(s): "The version of C:\Program Files (x86)\WinSCP\winscp.exe (6.3.3.0) does n ot match version of this assembly C:\Program Files\WindowsPowerShell\Modules\WinSCP\6.3.2.0\lib\net40\WinSCPnet.dll (6.3 .2.0)."

Example

$Session = New-Object WinSCP.Session
$SessionOptions = New-Object WinSCP.SessionOptions
$SessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$SessionOptions.HostName = $SFTPSite
$SessionOptions.UserName = $SFTPUser
[Password and KEYS] 

try {
    # Herstellen der Verbindung zum SFTP Server
    $session.Open($sessionOptions)

    # Hinterlegen der Session in allen CMDlets
    Get-Command -Module WinSCP -ParameterName WinSCPSession | ForEach-Object {
        $Global:PSDefaultParameterValues.Remove("$($_.Name):WinSCPSession")
        $Global:PSDefaultParameterValues.Add("$($_.Name):WinSCPSession", $session)
    }

} catch {
    Write-Error $_
    $session.Dispose()
    exit(1)
}

Expected Output

Connection is established. Newer Version of the module is missing.

Actual Output

See Issue description

WinSCP-PowerShell Version

ModuleType Version    Name                                ExportedCommands                                             
---------- -------    ----                                ----------------                                             
Script     6.3.2.0    winscp                              {ConvertTo-WinSCPEscapedString, Copy-WinSCPItem, Get-WinSC...

Environment

Windows Server 2019 Datacenter

Name : ConsoleHost Version : 5.1.17763.5696 InstanceId : c99acbd0-8249-4011-8a17-d5c96ab0a5ad UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : de-DE CurrentUICulture : en-US PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy DebuggerEnabled : True IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace

tomohulk commented 5 months ago

im not exactly sure what is going on, but its referencing a different install of the WinSCP.exe in your Program Files directory. I ship the matching exe to the dll in the module path, and I tell the session to use that binary specifically, see https://github.com/tomohulk/WinSCP/blob/45c2940b38cf6c46ab57dee7c1a08821fd7cd416/WinSCP/Public/New-WinSCPSession.ps1#L81.

do you maybe have the WinSCP client running at the same time?

rik-e commented 5 months ago

The Client is not running.

We use Scriptrunner to schedule Powershell Scripts. This is why $PSScriptRoot seems to be not working. After I uninstalled WinSCP directly the error changes:

Write-Error: Exception calling "Open" with "1" argument(s): "The winscp.exe executable was not found at location of the 
assembly WinSCPnet (C:\Program Files\WindowsPowerShell\Modules\WinSCP\6.3.2.0\lib\net40), nor the entry assembly SRXPSHo
st (C:\Program Files\ScriptRunner\Service\Bin\WinPS), nor in an installation path. You may use Session.ExecutablePath pr
operty to explicitly set path to winscp.exe." (NotSpecified: (:) [Write-Error], WriteErrorException)
Exception calling "Open" with "1" argument(s): "The winscp.exe executable was not found at location of the assembly WinS
CPnet (C:\Program Files\WindowsPowerShell\Modules\WinSCP\6.3.2.0\lib\net40), nor the entry assembly SRXPSHost (C:\Progra
m Files\ScriptRunner\Service\Bin\WinPS), nor in an installation path. You may use Session.ExecutablePath property to exp
licitly set path to winscp.exe."

As I get it there are three options:

tomohulk commented 5 months ago

ill have to think about this for a little bit, to me this feels like an issue with Script Runner and not the WinSCP module.

rik-e commented 5 months ago

I was able to get around this like that:

$exe_path = (Get-Module WinSCP).ModuleBase + "\bin\WinSCP.exe"

$Session = New-Object WinSCP.Session
$Session.ExecutablePath = $exe_path
$SessionOptions = New-Object WinSCP.SessionOptions
...

This would avoid the need to adjust the module.

tomohulk commented 4 months ago

I added two parameters to New-WinSCPSession, DisableVersionCheck so you don't have to have matching versions of the dll and exe. And ExecutablePath, this allows you to specify a full path to the .exe. Can you test this out in your PSScriptRunner and see if it works for your situation? You'll have to download the source code here, I haven't pushed it to the gallery yet. I also updated the WinSCP Core from 6.3.2 to 6.3.3. Please let me know how it works out, and I'll push it to the PSGallery. Thanks.

tomohulk commented 4 months ago

sorry for all those commits, trying to work out some dynamic mapping of parameters to properties, but its all good now, please let me know if this works for you're situation. Thanks!

rik-e commented 4 months ago

The test is scheduled for tomorrow. I'll let you know the result.

rik-e commented 4 months ago

I was able to use New-WinSCPSession -DisableVersionCheck $true without any error. Looks good to me.

tomohulk commented 4 months ago

Thanks, all my tests are passing the updated module has been published to the PSGallery.