Skip to content

An incidentally [psobject]-wrapped strongly typed array isn't recognized as such in argument-based parameter binding #21496

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

Note:

# Create and import a sample cmdlet that requires [byte[]] input
Add-Type @'
using System.Management.Automation;
[Cmdlet("Get", "Foo")]
public class GetFooCommand : PSCmdlet
{
    [Parameter(Mandatory=true,ValueFromPipeline=true)]
    public byte[] InputObject;

    protected override void ProcessRecord()
    {
    }
}
'@ -PassThru | ForEach-Object Assembly | Import-Module

# Create two instances of a large byte array, one without and
# one with a [psobject] wrapper
$byteArrayUnwrapped = [byte[]]::new(64mb)
$byteArrayWrapped = New-Object byte[] 64mb  # equivalent, but with implicit [psobject] wrapper

'--- unwrapped'
Get-Foo -InputObject $byteArrayUnwrapped
'--- wrapped'
Get-Foo -InputObject $byteArrayWrapped # !! Excessively slow.
'--- done'

Note that parameter binding via the pipeline is not affected; that is,
, $byteArrayWrapped | Get-Foo works as intended (note the need to wrap $byteArrayWrapped in an aux., transitory array via the unary form of , so as to ensure that it is passed as a single object).

Expected behavior

Both calls should return virtually instantly, given that the argument type exactly matches the parameter type and that the cmdlet is otherwise a no-op.

Actual behavior

The second call, due to the invisible [psobject] wrapper resulting from use of New-Object to construct the array, takes an excessive amount of time to complete.

Error details

No response

Environment data

PowerShell 7.5.0-preview.2

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions