Cannot Generate ParameterSetMetadata While Programmatically Creating A Parameter Block

Posted by Steven Murawski on Stack Overflow See other posts from Stack Overflow or by Steven Murawski
Published on 2010-03-17T21:58:06Z Indexed on 2010/03/17 22:01 UTC
Read the original article Hit count: 192

I'm trying to programmatically create a parameter block for a function ( along the lines of this blog post ).

I'm starting with a CommandMetadata object (from an existing function). I can create the ParameterMetadata object and set things like the ParameterType, the name, as well as some attributes.

The problem I'm running into is that when I use the GetParamBlock method of the ProxyCommand class, none of my attributes that I set in the Attributes collection of the ParameterMetadata are generated.

The problem this causes is that when the GetParamBlock is called, the new parameter is not annotated with the appropriate Parameter attribute.

Example:

function test 
{
    [CmdletBinding()]
    param (
    [Parameter()]
    $InitialParameter)

    Write-Host "I don't matter."
}

$MetaData = New-Object System.Management.Automation.CommandMetaData (get-command test)

$NewParameter = New-Object System.Management.Automation.ParameterMetadata 'NewParameter'

$NewParameter.ParameterType = [string[]]

$Attribute = New-Object System.Management.Automation.ParameterAttribute 
$Attribute.Position = 1
$Attribute.Mandatory = $true
$Attribute.ValueFromPipeline = $true

$NewParameter.Attributes.Add($Attribute)
$MetaData.Parameters.Add('NewParameter', $NewParameter)


[System.Management.Automation.ProxyCommand]::GetParamBlock($MetaData) 

© Stack Overflow or respective owner

Related posts about powershell-v2.0

Related posts about powershell