0

I am trying to load the WinSCP .DLL into a script so that I can use various attributes/Methods from the library. I am struggling because the example on the WinSCP site results in a "TypeNotFound" error when you actually try to use it. So far I have tried and failed to create a module/manifest to do so, and am now trying to simply load the DLL in a script and then load that script in my main script (because apparently this makes sense in PowerShell..?) I have been referred to this answer by Takophiliac, but This answer is not verbose enough for me and I assume some others to follow.

Please can someone just show me a simple example of loading a DLL library so that I can use the various attributes/methods in my main script without getting "TypeNotFound" errors? Please.

Edit: adding example

 [void] CreateSession() {
    try {
        # Load WinSCP .NET assembly
        Add-Type -Path (Join-Path $PSScriptRoot "\winscp\WinSCPnet.dll")
        # Setup session options
        $this.sessionOptions = New-Object WinSCP.SessionOptions -Property @{
            Protocol = [WinSCP.Protocol]::Sftp 

The issue is that [WinSCP.Protocol] causes a parsing error, as [WinSCP.Protocol] is apparently not known at parsing time? I dont understand how anyone really uses the example where they just use Add-Type, because after doing that you still get "TypeNotFound" errors everywhere you try to use the attributes/methods of the library.

7
  • In case this comes up, I need access to multiple methods in the WinSCP library, for example: [WinSCP.Protocol], [WinSCP.SynchronizationMode], [WinSCP.SynchronizationAction]. So, ideally the answer would give me access to [WinSCP] so that I can access all the attributes/methods within the library. I hope that makes sense, I dont use PowerShell as often as Python. And I do appreciate any help. Commented Sep 8, 2021 at 13:26
  • Sorry about that. I just added the example. I don't really see how my code is more complex. I'm just trying to sync a local and remote folder. If this is complex then I should just switch languages and libraries... Commented Sep 8, 2021 at 13:33
  • Your code is complex because you use classes (what you have never mentioned here), as @mklement0 answers at stackoverflow.com/q/69019962/850848#69104097 Commented Sep 8, 2021 at 13:41
  • So if I drop the classes and just use functions this should work? Commented Sep 8, 2021 at 13:42
  • 1
    I believe it would. @mklement0 pointed you to that fact a week ago already: stackoverflow.com/q/42837447/850848Powershell: Unable to find type when using PS 5 classes. Commented Sep 8, 2021 at 13:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.