Skip to content

PSCommand.Clone strips CommandInfo from inner Command objects #12297

@SeeminglyScience

Description

@SeeminglyScience

Steps to reproduce

$ps = $ps2 = $null
try {
    $ps = [powershell]::Create('CurrentRunspace')
    $cmdlet = [System.Management.Automation.CmdletInfo]::new('un-resolvable', [Microsoft.PowerShell.Commands.FormatDefaultCommand])
    $null = $ps.AddCommand($cmdlet).AddParameter("InputObject", 'test')

    $ps2 = [powershell]::Create('CurrentRunspace')

    # Setter for "Commands" calls PSCommand.Clone()
    $ps2.Commands = $ps.Commands

    $ps.Invoke()
    $ps2.Invoke()
} finally {
    if ($null -ne $ps2) {
        $ps2.Dispose()
    }

    if ($null -ne $ps) {
        $ps.Dispose()
    }
}

Expected behavior

test
test

Actual behavior

test
MethodInvocationException:
Line |
  13 |      $ps2.Invoke()
     |      ~~~~~~~~~~~~~
     | Exception calling "Invoke" with "0" argument(s): "The term 'un-resolvable' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.0-preview.1
PSEdition                      Core
GitCommitId                    7.1.0-preview.1
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

Fix

The clone constructor for SMAR.Command doesn't copy the value of the CommandInfo property from the command to be cloned.

/cc @TylerLeonhardt

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 aResolution-FixedThe issue is fixed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions