PowerShell / PowerShell/PowerShell

The `PSDefaultValue` attribute's `Value` named parameter can cause `Get-Help` to fail if defined as anything other than a string

Open
#24,967 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs-Triage WG-Interactive-Console WG-NeedsReview
Dominant language
C#
Stars
55.5k
Forks
8.5k
Avg merge
1d 2h
Merged PRs (30d)
88

Description

Prerequisites
Steps to reproduce

According to the PowerShell documentation, the Value named parameter which can be passed to PSDefaultValue() is defined as:

Type: object

This argument specifies a value that is intended to be the default value of a parameter. The value is used in an implementation-defined manner.

Windows PowerShell: The value is used as part of the description of the parameter for the help topic displayed by the Get-Helpcmdlet when the Help property is not specified.

Since it seems to be defined as an arbitrary object, I imagine that one could for instance set the default value as:

Value = [uri]'https://google.com'

However, given a script Get-Foo.ps1 defined as:

<#
    .SYNOPSIS
    Example help
#>
[CmdletBinding()]
[OutputType([void])]
param (
    [PSDefaultValue(Value = [uri]'https://example.com']
    [uri]
    $Baz
)

process {
    Write-Output $Baz
}

Running Get-Help Get-Foo.ps1 -Full causes the Cmdlet to emit an error.

Expected behavior
PS> Get-Help Get-Foo.ps1 -Full


NAME
    get-foo.ps1

SYNOPSIS
    Example help


SYNTAX
    get-foo.ps1 [[-Baz] <Uri>] [<CommonParameters>]


DESCRIPTION


PARAMETERS
    -Baz <Uri>

        Required?                    false
        Position?                    1
        Default value                <Stringified version of the argument passed to the "Value" named parameter>
        Accept pipeline input?       false
        Aliases
        Accept wildcard characters?  false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more
    information, see
        about_CommonParameters
    (https://go.microsoft.com/fwlink/?LinkID=113216).

INPUTS

OUTPUTS
    System.Void



RELATED LINKS
Actual behavior
PS> Get-Help Get-Foo.ps1 -Full

Get-Help: Cannot find an overload for ".ctor" and the argument count: "1".
Error details
Message     : Cannot find an overload for ".ctor" and the argument
count: "1".
    Source      : System.Management.Automation
    HResult     : -2146233087
    StackTrace  :
   at
System.Management.Automation.Language.Compiler.GetAttribute(AttributeAst
attributeAst)
   at System.Management.Automation.Language.Compiler.GetRuntimeDefinedParam
eter(ParameterAst parameterAst, Boolean& customParameterSet, Boolean&
usesCmdletBinding)
   at System.Management.Automation.Language.Compiler.GetParameterMetaData(R
eadOnlyCollection`1 parameters, Boolean automaticPositions, Boolean&
usesCmdletBinding)
   at
System.Management.Automation.CompiledScriptBlockData.InitializeMetadata()
   at System.Management.Automation.CompiledScriptBlockData.get_CmdletBindin
gAttribute()
   at System.Management.Automation.CommandMetadata..ctor(ScriptBlock
scriptblock, String commandName, ExecutionContext context)
   at System.Management.Automation.ExternalScriptInfo.get_CommandMetadata()
   at System.Management.Automation.HelpCommentsParser..ctor(CommandInfo
commandInfo, List`1 parameterDescriptions)
   at System.Management.Automation.HelpCommentsParser.CreateFromComments(Ex
ecutionContext context, CommandInfo commandInfo, List`1 comments, List`1
parameterDescriptions, Boolean dontSearchOnRemoteComputer, String&
helpFile, String& helpUriFromDotLink)
   at
System.Management.Automation.CommandHelpProvider.GetHelpInfo(CommandInfo
commandInfo, Boolean reportErrors, Boolean searchOnlyContent)
   at
System.Management.Automation.CommandHelpProvider.SearchHelp(HelpRequest
helpRequest, Boolean searchOnlyContent)+MoveNext()
   at System.Management.Automation.HelpSystem.SearchHelp(HelpRequest
helpRequest)+MoveNext()
   at System.Management.Automation.HelpSystem.DoGetHelp(HelpRequest
helpRequest)+MoveNext()
   at Microsoft.PowerShell.Commands.GetHelpCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Get-Help], MethodException
FullyQualifiedErrorId :
MethodCountCouldNotFindBest,Microsoft.PowerShell.Commands.GetHelpCommand
InvocationInfo        :
    MyCommand        : Get-Help
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 2
    Line             : get-help get-foo.ps1 -Full
    Statement        : get-help get-foo.ps1 -Full
    PositionMessage  : At line:1 char:1
                       + get-help get-foo.ps1 -Full
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : get-help
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
Environment data
Name                           Value
----                           -----
PSVersion                      7.6.0-preview.2
PSEdition                      Core
GitCommitId                    7.6.0-preview.2
OS                             Microsoft Windows 10.0.26100
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Get-Help failure with the PSDefaultValue(Value = [uri]'https://example.com') script shown in the issue. Trace the metadata path through Compiler.GetAttribute and verify that Get-Help -Full succeeds for non-string Value arguments while displaying a stringified default value.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.