No need to register the assembly for use from PowerShell using Add-Type
.
I got it working finally.
My apology for jumping between the 2 posts.
I removed the "Block" on the DLL properties and re-registered it.
I removed the -ComObject.
Before the Add-Type, I do CD to the folder where the DLL is installed.
Now the command is simply the DLL name.
Thanks for you assistance and patience.
Can you please stop skipping between the two topics?
I never wrote that you should not use Add-Type
.
I've asked you (in the other topic), why do you instantiate the class via COM - The -ComObject
switch.
Though the switch is not anymore in the latest code you have posted. But you didn't show us, what problem (if any) do you have with the latest code. It must be a different problem, than you had with the previous code.
I ran the same code on my Windows 7 64bit PC and it works fine, but does not work on the Windows 2012 R2 64bit server.
This is directly from the "Using WinSCP .NET Assembly from PowerShell"
It clearly shows the 'Add-Type' command.
This example is functionally equivalent to overall C# example for WinSCP .NET assembly.
There are also other PowerShell examples.
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "example.com"
UserName = "user"
Password = "mypassword"
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}
I unblocked the DLL and this is what I get when I attempt to run the PS1 script:
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$sessionOptions = New-Object -ComObject WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "myserver"
UserName = "myuser"
Password = "mypassword"
SshHostKeyFingerprint = "ssh-rsa 2048 fc:44:00:23:f2:8a:aa:00:64:f2:d8:36:4c:2e:6d:f1"
}
New-Object : Retrieving the COM class factory for component with CLSID {2D4EF368-EE80-4C15-AE77-D12AEAF4B00A} failed due to the following error:
80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
At line:19 char:19
+ $sessionOptions = New-Object -ComObject WinSCP.SessionOptions -Property @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (Smile [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
I have upgraded to WINSCP 5.9.5 Automation. My scripts all fail with the Add-type command:
Add-Type : Could not load file or assembly 'file:///C:\Program Files (x86)\WinSCP\WinSCPnet.dll' or one of its dependencies.
Operation is not supported. (Exception from HRESULT: 0x80131515)
After that error, none of the later winscp calls within the script will function.
Someone, please send me some advice on how to resolve this.
Note: my server is Windows 2012-R2 64bit.