SecureString in file "randomly" stops working SecureString in file "randomly" stops working powershell powershell

SecureString in file "randomly" stops working


It's really kind of odd the to decrypt just stops working, that sounds environmental. However, why are you doing this, this way...

Write-Output "Please enter the username of the SFTP user."$username = Read-HostWrite-Output "Please enter the password of the SFTP user"$password = Read-Host -AsSecureString$username | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "$scriptroot\Credentials.dat"$password | ConvertFrom-SecureString | Out-File -Append "$scriptroot\Credentials.dat"

... vs going one fo these routes.

$UserCreds = Get-Credential -Message 'Please enter the username of the SFTP credentials'$UserCreds.GetNetworkCredential().UserName$UserCreds.GetNetworkCredential().Password<## Resultspostanotepassword#>

Or this way,

Get-Credential -Message 'Please enter the username of the SFTP credentials.' | Export-Clixml -Path 'D:\temp\SFTPUserCreds.xml'Get-Content -Path 'D:\temp\SFTPUserCreds.xml'<## Results<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">  <Obj RefId="0">    <TN RefId="0">      <T>System.Management.Automation.PSCredential</T>      <T>System.Object</T>    </TN>    <ToString>System.Management.Automation.PSCredential</ToString>    <Props>      <S N="UserName">postanote</S>      <SS N="Password">01000000d08c9ddf0115d1118c7a00c04fc297eb01000000bd0a9dd27ffa41419fb2b289838ebe6400000000020000000000106600000001000020000000ad2e4c96413a3e93e461f600e98f72aa5e3493bde41ac40491bfd4bedc462152000000000e8000000002000020000000e0016657a9f6e545b55876fa8095143c08046f1361baec0f693a1e823cc6ee0d200000006949857c09b23099dab66fdc3a7e4f7637970dbd3aa13be11fda7c8de63df70e40000000f9a266cbb34440f64d9a2bdeaaec3a6cda483138e0be29323ca0a523ac475e169793557e74dd208e3d4292aa4fbe5b90fc7023d41c4dd6d01f819366e0587885</SS>    </Props>  </Obj></Objs>#>

no decrypt needed, just pass the variable names

($UserCreds = Import-Clixml -Path 'D:\Temp\SFTPUserCreds.xml')<## ResultsUserName                      Password--------                      --------postanote System.Security.SecureString#>

Or this way

Get-Credential -Message 'Please enter the username of the SFTP credentials.' | Export-PSCredential -RegistryPath 'HKCU:\software\SFTP' -Name SFTPUserCreds$UserCred = Import-PSCredential -RegistryPath 'HKCU:\Software\SFTP' -Name SFTPUserCreds# Or just use Windows Credential ManagerFind-Module -Name '*CredentialManager*' | Format-Table -AutoSizeVersion Name                          Repository Description                                                                        ------- ----                          ---------- -----------                                                                        2.0     CredentialManager             PSGallery  Provides access to credentials in the Windows Credential Manager                   ...

So, even based on what you've read about SecureString, it's still a thing for most folks, though based on what I show above, I am not sure why.

Anyway, give one or more of the above a shot to see if that allows you more consistancy.