Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Re: PowerShell SecurePassword

Thank you very much. That was the solution.

Re: PowerShell SecurePassword

It's just a wrong PowerShell code syntax.
This is correct syntax:
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    SecurePassword = ConvertTo-SecureString $config.Configuration.Password

PowerShell SecurePassword

Good Morning,
I want to copy files from Windows Server to SSH Server via WinSCP Script. My goal is to encrypt the login password of the SSH server in the script. I tried to encrypt the password as in this tutorial:

First I converted the plaintext password in a separate PowerShell with this command:
Read-Host -AsSecureString | ConvertFrom-SecureString

Now I have copied the generated string into my XML file:

now I started my PowerShell script:
This is my script:
# Read XML configuration file
    [xml]$config = Get-Content "C:\temp\config.xml"
    # Load WinSCP .NET assembly
    Add-Type -Path "path-to-winscp\WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    $sessionOptions.SecurePassword = ConvertTo-SecureString $config.Configuration.Password
        Protocol = [WinSCP.Protocol]::Scp
        HostName = "my-ip-address"
        UserName = $config.Configuration.UserName
        Password = $sessionOptions.SecurePassword
        SshHostKeyFingerprint = "ssh-ed25519 255 my-finger-print"
    $session = New-Object WinSCP.Session
        # Connect
        # Upload files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
        $transferResult =
            $session.PutFiles("my-source-path\myfile-*", "my-destination-path", $False, $transferOptions)
        # Throw on any error
        # Print results
        foreach ($transfer in $transferResult.Transfers)
            Write-Host "Upload of $($transfer.FileName) succeeded"
        # Disconnect, clean up
    exit 0
    Write-Host "Error: $($_.Exception.Message)"
    exit 1

I get the following error:
Error: The System.Security.SecureString element was not found for the specified .NET object.

What did I do wrong?