Post a reply

Options
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)

Options

Topic review

tbowar

Fixed

Martin,

I did paste your test code in and it showed that the $config.Configuration.Password variable matched the actual password.

To be honest, I am not sure why it didn't work, all I know is that I got an error when trying
$session.Open($sessionOptions)
session.Options with $config.Configuration.Password. When I changed the code a little more, it started working.

Thank you for your response.
martin

Re: Powershell example in Protecting credentials used for automation doesn't work

What does it mean "won't open"? Please be more specific.

Definitely, this is not about "WinSCP substituting $config.Configuration.Password correctly for the actual password". The $config.Configuration.Password must be containing a wrong password in the first place. There might be a small difference that you do not notice (like a whitespace or a wrong encoding).

Compare outputs of:
Write-Host ([System.Text.Encoding]::UTF8).GetBytes("actualpasswordstring")
Write-Host ([System.Text.Encoding]::UTF8).GetBytes($config.Configuration.Password)
tbowar

Powershell example in Protecting credentials used for automation doesn't work

After reading through the Protecting credentials used for automation section in the docs, I copied the Powershell code and created a script to test it:

# Read XML configuration file
[xml]$config = Get-Content "D:\test\configs\mediaserver.xml"
 
Write-Host $config.Configuration.Password
# This writes the correct password to the screen
 
# The session won't open with this code:
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "usefulmedia.com"
    UserName = "gplimpton"
    Password = $config.Configuration.Password
    PortNumber = "2222"
}
 
 
# The session opens correctly with this code:
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "usefulmedia.com"
    UserName = "gplimpton"
    Password = "actualpasswordstring"
    PortNumber = "2222"
}


Do I have a syntax error? Why isn't WinSCP substituting $config.Configuration.Password correctly for the actual password?