Skip to content

Using -AsPlainText parameter with PowerShell 5.1 fails #7

Description

@jrbilodeau

I tried using your module with PowerShell 5.1 on Windows 10, but whenever I use the parameter -AsPlainText it fails with the following error.

ConvertFrom-SecureString : A parameter cannot be found that matches parameter name 'AsPlainText'.
At C:\Program Files\WindowsPowerShell\Modules\PSPasswordGenerator\3.1.0\src\PSPasswordGenerator.psm1:159 char:41
+         Return (ConvertFrom-SecureString $ret -AsPlainText)
+                                               ~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertFrom-SecureString], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand

I looked up ConvertFrom-SecureString on Microsoft's site an it appears that -AsPlainText was not an option in PowerShell 5.1 but is in PowerShell 7 +

I wonder if it would be possible to detect the if a version of PowerShell less than 7 is being used and if so omit the -AsPlainText parameter, like this. I did a quick test and it seemed to work with PowerShell 5.1

If ($AsPlainText) {
	$result = If ($PSVersionTable.PSVersion.Major -ge 7){
		ConvertFrom-SecureString $ret -AsPlainText
	}
	Else {
		[System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
				[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($ret)
			)
	}
	
	Return ($result)
} Else {
	Return $ret
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions