Hi Martin,
Apologies, I’m following this article:
Protecting credentials used for automation
I’m using PowerShell and the
ConvertFrom-SecureString
method to encrypt the password in the XML file.
Here’s the contents of my configuration file:
<Configuration>
<UserName>myuser</UserName>
<Password>encrypted-password</Password>
</Configuration>
Here are the contents of my PowerShell script:
# Import the WinSCP assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" # Modify the path as per your WinSCP installation location
$config = Get-Content "C:\Temp\config.xml"
# Configure WinSCP session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "sftp.myserver.com"
UserName = $config.Configuration.UserName
SecurePassword = ConvertTo-SecureString $config.Configuration.Password
SshHostKeyFingerprint = "MySSHHostKeyFingerprint"
}
# Initialize a WinSCP session
$session = New-Object WinSCP.Session
# Open the WinSCP session with the specified session options
$session.Open($sessionOptions)
When running this I get the following output:
ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.
At C:\sftp.ps1:12 char:45
+ ... ecurePassword = ConvertTo-SecureString $config.Configuration.Password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
Exception calling "Open" with "1" argument(s): "SessionOptions.Password is set, but SessionOptions.UserName is not."
At C:\sftp.ps1:21 char:1
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
The password isn’t being picked up from the config.xml file
The
UserName
isn’t being picked up from the config.xml file
When I use the plain text password method I get the following:
Exception calling "Open" with "1" argument(s): "SessionOptions.Password is set, but SessionOptions.UserName is not."
At C:\sftp.ps1:21 char:1
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
In this instance just the following issue:
The
UserName
isn’t being picked up from the config.xml file
So to sumarise, I require some assistance to understand why:
The
UserName
isn’t being set
The
SecurePassword
method isn’t working.
Additionally, just a minor query, is it possible to store other parameters such as
HostName
and
SshHostkeyFingerprint
in the config.xml file?