Reading Password from File as Secure String

Advertisement

tkennedy
Joined:
Posts:
11

Reading Password from File as Secure String

I am doing some testing for some processes at work and I really like the possibility of using the .NET assembly. The issue I'm having is with the password. Our scripts are automated and the password is stored encrypted in a file. When I read the file the password is decrypted and converted to a SecureString. When I use the secure string I get an error:
Connection has been unexpectedly closed. Server sent command exit status 0.

but if I typed my password in the password variable, the script works fine. I'm using Powershell, is there any way to read a SecureString from disk and use it with the .NET assembly?

Here are examples of my session options:

# Setup session options
$SessionOptions = New-Object WinSCP.SessionOptions
$SessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$SessionOptions.HostName = $FTPHost
$SessionOptions.UserName = $FTPUser
$SessionOptions.Password = Read-EncString $ENCFILE
#$SessionOptions.Password = "[thepasswordintext]" # This line works 
$SessionOptions.SshHostKey = $SshHostKey

Thanksfor the help.

Reply with quote

Advertisement

tkennedy
Joined:
Posts:
11

Re: Reading Password from File as Secure String

martin wrote:

Sorry, no idea what Read-EncString does. Google does not give any results.

Wow, my mistake. That's what I get for asking questions late on Friday.

The correct command is ConvertTo-SecureString. "The ConvertTo-SecureString cmdlet converts encrypted standard strings into secure strings." My problem may be that I simply don't know how to use the secure string so please forgive me if I am asking this in the wrong place.

Reply with quote

martin
Site Admin
martin avatar

Re: Reading Password from File as Secure String

SessionOptions.Password is s string property, so cannot set it's value using some secure string object.

Reply with quote

Advertisement

You can post new topics in this forum