Skip to content

ProxyCommand breaks ValidateRange for enum-parameters #17546

@fflaten

Description

@fflaten

Prerequisites

Steps to reproduce

function Test-EnumValidation {
    param(
        [ValidateSet([Microsoft.PowerShell.ExecutionPolicy]::Unrestricted, [Microsoft.PowerShell.ExecutionPolicy]::Undefined)]
        [Microsoft.PowerShell.ExecutionPolicy]$Works,

        [ValidateRange([Microsoft.PowerShell.ExecutionPolicy]::Unrestricted, [Microsoft.PowerShell.ExecutionPolicy]::Undefined)]
        [Microsoft.PowerShell.ExecutionPolicy]$Fails
    )
}


$m = New-Object System.Management.Automation.CommandMetaData (Get-Command Test-EnumValidation)
$s = [System.Management.Automation.ProxyCommand]::Create($m)

$function:proxy = [scriptblock]::Create($s)

# fails with .ctor exception
proxy

# if we check the generated function, we see that `ValidateRange` got unquoted strings as arguments in the proxycommand
$s

...
param(
    [ValidateSet('Unrestricted','Undefined')]
    [Microsoft.PowerShell.ExecutionPolicy]
    ${Works},

    [ValidateRange(Unrestricted,Undefined)]
    [Microsoft.PowerShell.ExecutionPolicy]
    ${Fails})
....

Expected behavior

proxy should work without errors

Actual behavior

proxy throws MethodException due to missing overload.

Error details

Exception             :
    Type        : System.Management.Automation.MethodException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Cannot find an overload for ".ctor" and the argument count: "0".
            HResult : -2146233087
        CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : MethodCountCouldNotFindBest
        InvocationInfo        :
            ScriptLineNumber : 7
            OffsetInLine     : 5
            HistoryId        : -1
            Line             : [ValidateRange(Unrestricted, Undefined)]

            PositionMessage  : At line:7 char:5
                               +     [ValidateRange(Unrestricted, Undefined)]
                               +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    TargetSite  :
        Name          : GetAttribute
        DeclaringType : System.Management.Automation.Language.Compiler, System.Management.Automation, Version=7.2.4.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message     : Cannot find an overload for ".ctor" and the argument count: "0".
    Data        : System.Collections.ListDictionaryInternal
    Source      : System.Management.Automation
    HResult     : -2146233087
    StackTrace  :
   at System.Management.Automation.Language.Compiler.GetAttribute(AttributeAst attributeAst)
   at System.Management.Automation.Language.AttributeAst.GetAttribute()
   at System.Management.Automation.Language.Compiler.GetRuntimeDefinedParameter(ParameterAst parameterAst, Boolean& customParameterSet, Boolean& usesCmdletBinding)
   at System.Management.Automation.Language.Compiler.GetParameterMetaData(ReadOnlyCollection`1 parameters, Boolean automaticPositions, Boolean& usesCmdletBinding)
   at System.Management.Automation.Language.ScriptBlockAst.System.Management.Automation.Language.IParameterMetadataProvider.GetParameterMetadata(Boolean automaticPositions, Boolean& usesCmdletBinding)
   at System.Management.Automation.CompiledScriptBlockData.InitializeMetadata()
   at System.Management.Automation.CompiledScriptBlockData.get_ObsoleteAttribute()
   at System.Management.Automation.DlrScriptCommandProcessor.Init()
   at System.Management.Automation.CommandDiscovery.CreateCommandProcessorForScript(FunctionInfo functionInfo, ExecutionContext context, Boolean useNewScope, SessionStateInternal sessionState)
   at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin commandOrigin, Nullable`1 useLocalScope, SessionStateInternal sessionState)
   at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
   at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionCont
ext context)
   at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandR
edirections, FunctionContext funcContext)
   at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo          : NotSpecified: (:) [], MethodException
FullyQualifiedErrorId : MethodCountCouldNotFindBest
InvocationInfo        :
    ScriptLineNumber : 7
    OffsetInLine     : 5
    HistoryId        : -1
    Line             : [ValidateRange(Unrestricted, Undefined)]

    PositionMessage  : At line:7 char:5
                       +     [ValidateRange(Unrestricted, Undefined)]
                       +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.4
PSEdition                      Core
GitCommitId                    7.2.4
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions