Marc
Marc

Reputation: 14361

Get Selected Properties of Parameters of a Powershell CmdLet

Given any Cmdlet (Powershell v3) I want to get a list of the parameters with only selected properties (e.g. name and type). The output as JSON should something like:

[
        {
                "Name":  "Path",
                "ParameterType":  "string"
        },
        {
                "Name":  "Filter",
                "ParameterType":  "string"
        },
        {
                "Name":  "Recurse",
                "ParameterType":  "switch"
        }
]

I have the collection of parameters but I cannot seem to select only those parameters I want:

$cmd = gcm Get-ChildItem
$parameters = $cmd.Parameters.Values
for($i=0; $i -lt $parameters.Count; $i++) {
        $parameters[$i] = Select-Object Name,ParameterType -InputObject $parameters[$i]
}
$parameters | ConvertTo-JSON -depth 1

I get all parameters:

[
        {
                "Name":  "Path",
                "ParameterType":  "string",
                "ParameterSets":  "System.Collections.Generic.Dictionary`2[System.String,System.Management.Automation.ParameterSetMetadata]",
                "IsDynamic":  false,
                "Aliases":  "",
                "Attributes":  "System.Management.Automation.ParameterAttribute System.Management.Automation.ArgumentTypeConverterAttribute",
                "SwitchParameter":  false
        },
        {
                "Name":  "Filter",
                "ParameterType":  "string",
                "ParameterSets":  "System.Collections.Generic.Dictionary`2[System.String,System.Management.Automation.ParameterSetMetadata]",
                "IsDynamic":  false,
                "Aliases":  "",
                "Attributes":  "System.Management.Automation.ParameterAttribute System.Management.Automation.ArgumentTypeConverterAttribute",
                "SwitchParameter":  false
        },
...
]

Upvotes: 0

Views: 532

Answers (1)

Marc
Marc

Reputation: 14361

$cmd = gcm Get-ChildItem
$cmd.Parameters.Values |
    Select Name,ParameterType,SwitchParameter |
   ConvertTo-Json -depth 1

Result:

[
        {
                "Name":  "Path",
                "ParameterType":  "string[]",
                "SwitchParameter":  false
        },
        {
                "Name":  "LiteralPath",
                "ParameterType":  "string[]",
                "SwitchParameter":  false
        },
        {
                "Name":  "Filter",
                "ParameterType":  "string",
                "SwitchParameter":  false
        },
        ...
]

Upvotes: 2

Related Questions