Hi,
I am using WinSCP for an upload operation from powershell. I believe, to use the WinSCP assembly I have to register it in the system. So I copied Winscp.dll to C:\ and running the following command in command prompt:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe C:\WinSCP.dll /codebase /tlb
But, it's failing and returning error:
------
Microsoft (R) .NET Framework Assembly Registration Utility 4.0.30319.1
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.
RegAsm : error RA0000 : Could not load file or assembly 'file:///C:\WinSCP.dll'
or one of its dependencies. Operation is not supported. (Exception from HRESULT:
0x80131515)
------
Can anyone please help, how to fix this error?
Powershell Code i am going to use:
------------------
try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("WinSCP.dll") | Out-Null
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles("b:\toupload\*", "./", $FALSE, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
------------------