Skip to content

Convert-ToSecureString fails on PS Core 7 Preview 4 - Previously working on PS Core 6.1 and WP 5.x #10561

@johnmccrae

Description

@johnmccrae

Decrypt an encrypted password

            $KeyFile = "$ENV:UserProfile\.pass\aes.key"
            $Key = Get-Content $KeyFile
        $datafile = Join-Path -Path $PsScriptRoot -ChildPath 'encryptedthings.csv'

        (import-csv $datafile) |
            Foreach-Object {

            if($_.AccountPassword)
                {
                    $Securestring = ConvertTo-SecureString $_.AccountPassword -Key $key
                    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Securestring)
                    $DecryptedPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
                }
            else {
                $DecryptedPassword = ''
            }

Expected behavior

This code snippet works and has been working for some time on Windows PowerShell 5.x and PowerShell Core 6.x on OSX and Linux.

Actual behavior

ConvertTo-SecureString : Input string was not in a correct format.
At C:\Program Files\PowerShell\Modules\mymodule\0.0.0.2\Public\Get-stuff.ps1:53 char:37
+ ...   $Securestring = ConvertTo-SecureString $_.AccountPassword -Key $key
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [ConvertTo-SecureString], FormatException
+ FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.4
PSEdition                      Core
GitCommitId                    7.0.0-preview.4
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions