0
votes

I'm trying to add C# type to PowerShell as follows:

Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")

And I get the following error:

Add-Type : Cannot bind parameter 'Path' to the target. Exception setting "Path":
"Cannot find path 'D:\Source\Repos\....\TestDataAccess\WinSCPnet.dll' because it
does not exist."
At D:\Source\Repos\....\TestDataAccess\WinSCPFiles.ps1:8 char:16
+ Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Add-Type], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.AddTypeCommand
1
Does the file D:\Source\Repos....\TestDataAccess\WinSCPnet.dll really exist? - NamiraJV
It looks like the issue is with your $assemblyPath parameter - can you specify how was it initialized? - Amittai Shapira
Yes, sorry for the delay in replying. It worked after i've set the path variables to this folder. Many thanks - King81

1 Answers

1
votes

Have you followed all the directions / options provided by the vendor/author.

https://winscp.net/eng/docs/library_powershell

Loading Assembly

PowerShell script needs to load the assembly before it can use classes the assembly exposes. To load assembly use Add-Type cmdlet.4)

Add-Type -Path "WinSCPnet.dll"

Had you need to run the script from other directory, you need to specify a full path to the assembly. You can derive the path from the script file path using $PSScriptRoot automatic variable:5)

Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")

If you are writing a script that you plan to use as a WinSCP extension (a custom command), you can use the copy of the assembly installed with WinSCP. In that case you can use the WINSCP_PATH environment variable to resolve the path to the assembly. To allow the script run even outside of WinSCP, you should fall back to the $PSScriptRoot approach (as above), if the variable is not defined:

$assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot }
Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")