Skip to content

Instantly share code, notes, and snippets.

@aetos382
Created August 28, 2020 02:26
Show Gist options
  • Save aetos382/9a6989cd9b9c4c3d7f8f4f18830ef903 to your computer and use it in GitHub Desktop.
Save aetos382/9a6989cd9b9c4c3d7f8f4f18830ef903 to your computer and use it in GitHub Desktop.
function Get-CommandParameter {
param(
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[string] $Name,
[ValidateNotNullOrEmpty()]
[string[]] $ParameterSetName = @(),
[switch] $IncludeCommonParameters)
begin {
$commonParameterNames =
@([System.Management.Automation.Cmdlet]::CommonParameters) +
@([System.Management.Automation.Cmdlet]::OptionalCommonParameters)
}
process {
$commands = Get-Command -Name $Name
foreach ($command in @($commands)) {
$parameterSets = $command.ParameterSets
foreach ($parameterSet in $parameterSets) {
$psName = $parameterSet.Name
if ($ParameterSetName) {
if ($psName -notin $ParameterSetName) {
continue
}
}
$parameters = $parameterSet.Parameters
foreach ($parameter in $parameters) {
$parameterName = $parameter.Name
if (!$IncludeCommonParameters) {
if ($parameterName -in $commonParameterNames) {
continue
}
}
$attributes = $parameter.Attributes
$parameterAttribute = $attributes |
Where-Object -FilterScript {
($_ -is [System.Management.Automation.ParameterAttribute]) -and
($_.ParameterSetName -eq $psName)
}
$mandatory = $parameterAttribute.Mandatory
$result = [PSCustomObject] @{
# Command = $command
# CommandName = $command.Name
# Parameter = $parameter
# ParameterAttribute = $parameterAttribute
# ParameterSet = $parameterSet
Name = $parameter.Name
ParameterType = $parameter.ParameterType
ParameterSetName = $psName
Mandatory = $mandatory
}
$result | Add-Member -TypeName 'CommandParameterInfo'
$result | Write-Output -NoEnumerate
}
}
}
}
}
<#
Get-CommandParameter -Name Get-Command |
Sort-Object -Property 'ParameterSetName', 'Name' |
Format-Table -GroupBy 'ParameterSetName'
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment