Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using -AsPlainText parameter with PowerShell 5.1 fails #7

Open
jrbilodeau opened this issue Nov 1, 2024 · 2 comments · May be fixed by #8
Open

Using -AsPlainText parameter with PowerShell 5.1 fails #7

jrbilodeau opened this issue Nov 1, 2024 · 2 comments · May be fixed by #8
Assignees
Labels
bug Something isn't working downlevel compatibility Issues with Windows PowerShell 5.1. good first issue Good for newcomers
Milestone

Comments

@jrbilodeau
Copy link

jrbilodeau commented Nov 1, 2024

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
}
@rhymeswithmogul
Copy link
Owner

Hey, that's a great idea! I don't test it well enough on that old version. I’ll see if I can fix this over the weekend.

@rhymeswithmogul rhymeswithmogul added this to the v3.2.0 milestone Nov 3, 2024
@rhymeswithmogul rhymeswithmogul self-assigned this Nov 3, 2024
@rhymeswithmogul rhymeswithmogul added the bug Something isn't working label Nov 3, 2024
rhymeswithmogul added a commit that referenced this issue Nov 3, 2024
Versions 3.0.0 to 3.1.0 (inclusive)  of this module contained a bug that
prevented it from working correctly on Windows PowerShell 5.1.  The call
to `ConvertFrom-SecureString` used the parameter `-AsPlainText`, which
does not exist before PowerShell 7.0.

This commit fixes that by checking to see if `ConvertFrom-SecureString`
supports the `-AsPlainText` parameter.  If so, it is called with it, and
if not, it's called without it.

Fixes #7
@rhymeswithmogul
Copy link
Owner

I've committed a change that fixes this behavior. Instead of doing a version check like you suggested, I checked to see if ConvertFrom-SecureString has a parameter called -AsPlainText, and uses it if it exists. It works on PowerShell 7.4, but I won't be able to test it on Windows PowerShell 5.1 until tomorrow. Let me know if you can download and test it before then.

@rhymeswithmogul rhymeswithmogul linked a pull request Nov 3, 2024 that will close this issue
@rhymeswithmogul rhymeswithmogul linked a pull request Nov 3, 2024 that will close this issue
@rhymeswithmogul rhymeswithmogul added downlevel compatibility Issues with Windows PowerShell 5.1. good first issue Good for newcomers labels Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working downlevel compatibility Issues with Windows PowerShell 5.1. good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants