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

Advertisement

tbowar
Joined:
Posts:
3
Location:
Minnesota, USA

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?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,603
Location:
Prague, Czechia

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)

Reply with quote

tbowar
Joined:
Posts:
3
Location:
Minnesota, USA

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.

Reply with quote

Advertisement

You can post new topics in this forum