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

Write-PSFMessage [switch] input handling for -EnableException #655

Open
mattcargile opened this issue Jan 24, 2025 · 1 comment
Open

Write-PSFMessage [switch] input handling for -EnableException #655

mattcargile opened this issue Jan 24, 2025 · 1 comment

Comments

@mattcargile
Copy link

mattcargile commented Jan 24, 2025

Looks like an issue with pwsh 7.4.6 binder. Just thought I would point it out I guess because Stop-PSFFunction doesn't have this issue. Maybe creating a new type would help and not be breaking? I guess I can also just switch to using Stop-PSFFunction instead.

PSF Demo

& { param([switch]$a ) Write-PSFMessage 'hello' -EnableException $a } 

Generic Demo

Add-Type '
using System.Management.Automation;

[Cmdlet("Test", "Command")]
public sealed class TestCommand : PSCmdlet
{
    [Parameter]
    public bool EnableException;
}' -PassThru | Import-Module -Assembly { $_.Assembly }
Test-Command -EnableException ([switch]::new($false))

PSF Working Demo

& { param([switch]$a ) Stop-PSFFunction -Message 'hello' -EnableException $a } 
@FriedrichWeinmann
Copy link
Member

Heya, thanks for bringing this up!
Looks like I forgot to apply my fix to Write-PSFMessage, fixing that in the next release.

It's part of the type-transformation in Parameter binding being a bit inconsistent here - functions (such as Stop-PSFFunction) don't have the issue, Cmdlets however do. The implementation workaround (which I already added to Invoke-PSFProtectedCommand, but not to Write-PSFMessage, apparently) is a type transformation attribute:

$source = @'
using System.Management.Automation;
using PSFramework.Utility;
using System;

[Cmdlet("Test", "Command")]
public sealed class TestCommand : PSCmdlet
{
    [Parameter]
    [TypeTransformation(typeof(Boolean))]
    public bool EnableException;
}
'@
$ref = [appdomain]::CurrentDomain.GetAssemblies() | Where-Object Location
Add-Type $source -ReferencedAssemblies $ref -PassThru -IgnoreWarnings -WarningAction Ignore | Import-Module -Assembly { $_.Assembly }
Test-Command -EnableException ([switch]::new($false))

This assumes you have PSFramework already imported.

Will be fixed in the next release :)

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

No branches or pull requests

2 participants