Cannot Generate ParameterSetMetadata While Programmatically Creating A Parameter Block
- by Steven Murawski
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)