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.

CamParker
  • 105
  • 2
  • 9
  • 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. – CamParker Sep 08 '21 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... – CamParker Sep 08 '21 at 13:33
  • Your code is complex because you use classes (what you have never mentioned here), as @mklement0 answers at https://stackoverflow.com/q/69019962/850848#69104097 – Martin Prikryl Sep 08 '21 at 13:41
  • So if I drop the classes and just use functions this should work? – CamParker Sep 08 '21 at 13:42
  • 1
    I believe it would. @mklement0 pointed you to that fact a week ago already: https://stackoverflow.com/q/42837447/850848 – *Powershell: Unable to find type **when using PS 5 classes***. – Martin Prikryl Sep 08 '21 at 13:42
  • Well you are correct. without using classes it works just fine. I honestly feel the past answers do not offer enough clarification on ways to resolve this issue. Frankly, if someone had just said, "The easiest workaround is to just use functions and not classes in your code" then I would have done so a week ago. Regardless, thank you for your help. – CamParker Sep 08 '21 at 13:58
  • If you want to add an answer to that fact, then I will mark it as correct, and close this question. – CamParker Sep 08 '21 at 14:00

0 Answers0