简体   繁体   中英

Powershell: Dynamic Param that references a Static Param Errors when using Get-Help. How to Suppress error?

A Module Variable

$ModVar = @{Value = Key}

Errors when using static Param -Name , as a key reference.

PS> help Test-Function

InvalidOperation: 
Line |
 214 |              $ModItem = $ModVar[$Name]
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Index operation failed; the array index evaluated to null.

I believe this is because the Param -Name does not exist until execution time. The function otherwise works correctly when used, this error only happens when using Get-Help .

Is there a methodology for getting around the error?

I have tried:

If (!$Name) {$Name = "A_Key_in_ModVar"}

In an attempt to supply a dummy value when it doesn't detect a value in -Name , but this causes the function to always overwrite the value of -Name , even if the parameter -Name is provided with an explicit value from the terminal.

Gist Example: GitHub to Get-KeyAndPeeleSchoolName

Code Snippet (full example above):

DynamicParam {

        $ParameterName = 'School'

        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
        Write-Error $Name
        $attSet = $Table[$Name]
        Write-Error $attSet
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($attSet)
        $ValidateSetAttribute.ErrorMessage = "Value must be $attSet"
        $AttributeCollection.Add($ValidateSetAttribute)

    $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
    $ParameterAttribute.ParameterSetName = "ParamSet1"
    $AttributeCollection.Add($ParameterAttribute)

        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)

        $RuntimeParameterDictionary
    }

Edit 1: Other issues I noticed.

Values for -Name that contains spaces prevent the Dynamic Param block containing -School from working.

Also when using ArgumentCompleter on -Names , the possible values with complex punction fail to completely autocomplete. This is probably because of the matching behavior, but any suggestions for how to make this behave would be great.

Also using " " quotes around a parameter value also prevent the argument completer from working, any suggestions for this would also be great.

Wrapping the [RuntimeDefinedParameterDictionary] object and accompanying constructs in an IF statement resolved the issue. -Name does not exist pre-binding when -Name is not passed a value explicitly. It is also not available when Get-Help runs to analyze the makeup of the Function.

To resolve this, check for the existence of the parameter being referenced within the DynamicParam block to control its execution.

DynamicParam {

if ($Name) { #If $Name exist create Dynamic Param.

        $ParameterName = 'School'

        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
        
        $attSet = $Table[$Name]
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($attSet)
        $ValidateSetAttribute.ErrorMessage = "Value must be $attSet"
        $AttributeCollection.Add($ValidateSetAttribute)

        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.ParameterSetName = "ParamSet1"
        $AttributeCollection.Add($ParameterAttribute)

        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)

        $RuntimeParameterDictionary
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM