Problem using WinSCP .NET assembly

Advertisement

Jan Kreuz
Guest

Problem using WinSCP .NET assembly

Hello Martin,
I wanted to include .NET assembly into my script (which is just copying data from SFTP server).
I downloaded WinSCP-X.X.X-Automation.zip package and extracted it into my PowerShell script folder. When I launched the script I got following error message:
New-Object : The value supplied is not valid, or the property is read-only. Change the value, and then try again.
At C:\projects\AirFASE-script\copy-data-from-TVS_v1.1.ps1:21 char:19
+ $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Object], Exception
+ FullyQualifiedErrorId : SetValueException,Microsoft.PowerShell.Commands.NewObjectCommand
When I normally installed WinSCP, the script worked well. I need to have the script simply portable to different machine without WinSCP installation.

Can you pls. helo what could be wrong?
Thanks Jan

Reply with quote

Advertisement

vm11
Guest

Same problem

#Load the .NET assembly for WinSCP
Add-Type -Path "C:\Program Files (x86)\WinSCP Automation\WinSCPnet.dll"
 
#Import the CSV containing switch details and store it in a variable
$switches = Import-Csv -Path "C:\Network Switch Backup\switches.csv"
 
#Get the current system date in the format year/month/date which will be used to name the backup files
$date = Get-Date -Format yyyy-M-d
 
#Loop over the lines in the CSV
Foreach ($line in $switches) {
 
    #Define the folder to store the output in and create it if it does not exist (if the folder exists already this will generate a non-blocking error)
    $outputfolder = "C:\Network Switch Backup\Backups\" + $line.hostname + "\"
    New-Item $outputfolder -ItemType Directory
 
    #Define the path to store the result of the download
    $outputpath = $outputfolder + $date
 
    #Store the session details
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = $line.hostname
        UserName = $line.username
        Password = $line.password
        SshHostKeyFingerprint = $line.sshhostfingerprint
    }
 
    $session = New-Object WinSCP.Session
 
    #Connect to the host
    $session.Open($sessionOptions)
 
    #Define the transfer options
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
    #Download the startup-config (the result of the last 'write memory' from the switches CLI) and save it to the outputpath
    $transferResult = $session.GetFiles("/cfg/startup-config", $outputpath, $False, $transferOptions)
 
    #Disconnect from the server
    $session.Dispose()
}
Here is/are the error(s):
New-Object : Der angegebene Wert ist ungültig, oder die Eigenschaft ist schreibgeschützt. Ändern Sie den Wert, und wiederholen Sie den Vorgang.
In C:\Network Switch Backup\Backup Network Switches.ps1:26 Zeichen:19
+ $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-Object], Exception
    + FullyQualifiedErrorId : SetValueException,Microsoft.PowerShell.Commands.NewObjectCommand
 
Ausnahme beim Aufrufen von "Open" mit 1 Argument(en):  "Der Wert darf nicht NULL sein.
Parametername: sessionOptions"
In C:\Network Switch Backup\Backup Network Switches.ps1:37 Zeichen:1
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException
 
Ausnahme beim Aufrufen von "GetFiles" mit 4 Argument(en):  "Session is not opened"
In C:\Network Switch Backup\Backup Network Switches.ps1:44 Zeichen:1
+ $transferResult = $session.GetFiles("/cfg/startup-config", $outputpat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: Same problem

@vm11: The only validated property in that statement is SshHostKeyFingerprint. So your $line.sshhostfingerprint probably does not contain the host key fingerprint in a correct format. See

Reply with quote

Advertisement

You can post new topics in this forum