Search code examples
powershellautomationdependencieswinscpwindows-server-2019

Winscp assembly won't load in powershell: dependency missing


I'm encountering the following error when trying to run a script involving winscp automation.

Add-Type : Could not load file or assembly 
'file://\\~removed~\dfs\Repo\Scripts\ScheduledTasks\~removed~\WinSCP-Automation\WinSCPnet.dll' or one of its dependencies. 
Operation is not supported. (Exception from HRESULT: 0x80131515)
At \\~removed~\dfs\Repo\Scripts\ScheduledTasks\~removed~\Start-Upload.ps1:19 char:1
+ Add-Type -Path (Join-Path $env:WINSCP_PATH "WinSCPnet.dll")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

The code in question that gets this error:

#Dependencies 
$Path = '\\redacted\dfs\Repo\Scripts\ScheduledTasks\redacted'
Set-Location -Path $Path
$env:WINSCP_PATH = '.\WinSCP-Automation'
Add-Type -Path (Join-Path $env:WINSCP_PATH "WinSCPnet.dll")

Basically we run a powershell script in task scheduler which uploads some data somewhere else using winscp.

This script works fine on the previous task scheduler box which is windows 2012 R2. But we built a windows 2019 box, copied everything over and we get the error above.

The winscp automation is being run off of the UNC path via the script. I have also tried the latest version of the winscp assembly from that unc path but also tried loading it in to the script from a local directory on the box.

However I shake it it always gets this error. I have also installed versions of vs c++ redistributables that were on the 2012r2 box and confirmed the .net Framework versions.The DLL and all winscp files are unblocked.

I tried running dependency walker but it's an outdated tool that doesn't really work for 2019 so I used another modern version on GitHub which did show me that I had all the dependencies needed.

I must also add that they are both using powershell 5.1, but the 2012 R2 is using a slightly older release. Both have the winscp powershell module installed too. I've tried using the alt versions of the DLL in the folder and also tried adding to GC.

I'm completely baffled and right down the rabbit hole, any guidance or support would be greatly appreciated.


Solution

  • So I couldnt get it to work from UNC path with this but I was able to fix it using

    [Reflection.Assembly]::LoadFile('C:\Scripts\Redacted\WinSCP-Automation\WinSCPnet.dll')
    

    Instead of

    $env:WINSCP_PATH = '.\WinSCP-Automation'
    Add-Type -Path (Join-Path $env:WINSCP_PATH "WinSCPnet.dll")
    

    Not sure still why it works locally and not from the remote path and not sure why its working with this reflection assembly but I digress, the problem is solved.