PoshCode Logo PowerShell Code Repository

Get-Parameter by Joel Bennett 24 months ago (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1592"></script>download | new post

Enumerate the Parameters of a command by ParameterSet, including DynamicParameters. Includes a -Full output mode and support for specifying the module. The only Get-Parameter you need :)

  1. #Requires -version 2.0
  2. ##This is just script-file nesting stuff, so that you can call the SCRIPT, and after it defines the global function, it will call it.
  3. param (
  4.    [Parameter(Position=1,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
  5.    [string[]]$Name
  6. ,
  7.    [Parameter(Position=2,ValueFromPipelineByPropertyName=$true,Mandatory=$false)]
  8.    $ModuleName
  9. ,
  10.    [switch]$ShowCommon
  11. ,
  12.    [switch]$Full
  13. ,
  14.    [switch]$Passthru
  15. )
  16.  
  17.  
  18. function global:Get-Parameter {
  19. #.Synopsis
  20. # Enumerates the parameters of one or more commands
  21. #.Description
  22. # Lists all the parameters of a command, by ParameterSet, including their aliases, type, etc.
  23. #
  24. # By default, formats the output to tables grouped by command and parameter set
  25. #.Example
  26. #  Get-Command Select-Xml | Get-Parameter
  27. #.Example
  28. #  Get-Parameter Select-Xml
  29.  
  30. param (
  31.    [Parameter(Position=1,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
  32.    [string[]]$Name
  33. ,
  34.    [Parameter(Position=2,ValueFromPipelineByPropertyName=$true,Mandatory=$false)]
  35.    $ModuleName
  36. ,
  37.    [switch]$ShowCommon
  38. ,
  39.    [switch]$Full
  40. ,
  41.    [switch]$Passthru
  42. )
  43.    if($Passthru) {
  44.       $PSBoundParameters.Remove("Passthru")
  45.       Get-ParameterRaw @PSBoundParameters
  46.    } elseif(!$Full) {
  47.       $props = "Name", "Type", "ParameterSet", "Mandatory", "Provider"
  48.       Get-ParameterRaw @PSBoundParameters | Format-Table $props -GroupBy @{n="Command";e={"{0}`n   Set:     {1}" -f $_.Command,$_.ParameterSet}}
  49.    } else {
  50.       $props = "Name", "Aliases", "Type", "ParameterSet", "Mandatory", "Provider", "Pipeline", "PipelineByName", "Position"
  51.  
  52.       Get-ParameterRaw @PSBoundParameters | Format-Table $props -GroupBy @{n="Command";e={"{0}`n   Set:     {1}" -f $_.Command,$_.ParameterSet}}
  53.    }
  54. }
  55.  
  56. ## This is Hal's original script (modified a lot)
  57. Function global:Get-ParameterRaw {
  58. param (
  59.    [Parameter(Position=1,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
  60.    [string[]]$Name
  61. ,
  62.    [Parameter(Position=2,ValueFromPipelineByPropertyName=$true,Mandatory=$false)]
  63.    $ModuleName
  64. ,
  65.    [switch]$ShowCommon
  66. ,
  67.    [switch]$Full
  68. )
  69. BEGIN {
  70.    $PropertySet = @( "Name",
  71.                      "Aliases",
  72.                      @{n="Type";e={$_.ParameterType.Name}},
  73.                      "ParameterSet",
  74.                      @{n="Command";e={"{0}/{1}" -f $(if($command.ModuleName){$command.ModuleName}else{$Command.CommandType.ToString()+":"}),$command.Name}},
  75.                      @{n="Mandatory";e={$_.IsMandatory}},
  76.                      @{n="Provider";e={$_.DynamicProvider}},
  77.                      @{n="Pipeline";e={$_.ValueFromPipeline}},                    
  78.                      @{n="PipelineByName";e={$_.ValueFromPipelineByPropertyName}},
  79.                      "Position"
  80.                   )
  81.    if(!$Full) {
  82.       $PropertySet = $PropertySet[0,2,3,4,5,6]
  83.    }
  84.    function Join-Object {
  85.    Param(
  86.       [Parameter(Position=0)]
  87.       $First
  88.    ,
  89.       [Parameter(ValueFromPipeline=$true,Position=1)]
  90.       $Second
  91.    )
  92.       begin {
  93.          [string[]] $p1 = $First | gm -type Properties | select -expand Name
  94.       }
  95.       process {
  96.          $Output = $First | Select $p1
  97.          foreach($p in $Second | gm -type Properties | Where { $p1 -notcontains $_.Name } | select -expand Name) {
  98.             Add-Member -in $Output -type NoteProperty -name $p -value $Second."$p"
  99.          }
  100.          $Output
  101.       }
  102.    }
  103. }
  104. PROCESS{
  105.    foreach($cmd in $Name) {
  106.       if($ModuleName)   { $cmd = "$ModuleName\$cmd" }
  107.       $commands = @(Get-Command $cmd)
  108.  
  109.       foreach($command in $commands) {
  110.          # resolve aliases (an alias can point to another alias)
  111.          while ($command.CommandType -eq "Alias") {
  112.             $command = @(Get-Command ($command.definition))[0]
  113.          }
  114.          if (-not $command) { continue }
  115.  
  116.          $Parameters = @{}
  117.  
  118.          foreach($provider in Get-PSProvider) {
  119.             $drive = "{0}\{1}::\" -f $provider.ModuleName, $provider.Name
  120.             ## Write-Verbose ("Get-Command $command -Args $drive")
  121.            
  122.             $MoreParameters = Get-Command $command -Args $drive | Select -Expand Parameters
  123.             $Dynamic = $MoreParameters | Select -Expand Values | Where { $_.IsDynamic }
  124.             foreach($k in $Parameters.Keys){ $null = $MoreParameters.Remove($k) }
  125.             $Parameters += $MoreParameters
  126.            
  127.             # Write-Verbose "Drive: $Drive | Parameters: $($Parameters.Count)"
  128.             if($dynamic) {
  129.                foreach($d in $dynamic) {
  130.                   if(Get-Member -input $Parameters.($d.Name) -Name DynamicProvider) {
  131.                      Write-Debug ("ADD:" + $d.Name + " " + $provider.Name)
  132.                      $Parameters.($d.Name).DynamicProvider += $provider.Name
  133.                   } else {
  134.                      Write-Debug ("CREATE:" + $d.Name + " " + $provider.Name)
  135.                      $Parameters.($d.Name) = $Parameters.($d.Name) | Add-Member NoteProperty DynamicProvider @($provider.Name) -Passthru
  136.                   }
  137.                }
  138.             }
  139.          }
  140.          
  141.          
  142.          #Write-Output $Parameters
  143.        
  144.          foreach ($paramset in @($command.ParameterSets | Select -Expand "Name")){
  145.             foreach($parameter in $Parameters.Keys) {
  146.                if(!$ShowCommon -and ($Parameters.$Parameter.aliases -match "vb|db|ea|wa|ev|wv|ov|ob|wi|cf")) { continue }
  147.                # Write-Verbose "Parameter: $Parameter"
  148.                if($Parameters.$Parameter.ParameterSets.ContainsKey($paramset) -or $Parameters.$Parameter.ParameterSets.ContainsKey("__AllParameterSets")) {                  
  149.                   if($Parameters.$Parameter.ParameterSets.ContainsKey($paramset)) {
  150.                      $output = Join-Object $Parameters.$Parameter $Parameters.$Parameter.ParameterSets.$paramSet
  151.                   } else {
  152.                      $output = Join-Object $Parameters.$Parameter $Parameters.$Parameter.ParameterSets.__AllParameterSets
  153.                   }
  154.                   if($Full -and $output.Position -lt 0) {$output.Position = $null}
  155.                   $setName = $(if($paramset -eq "__AllParameterSets") { "Default" } else { $paramset })
  156.                   Write-Verbose "ParameterSet: $setName"
  157.                   $output = $output | Add-Member NoteProperty ParameterSet -value "$setName" -Passthru
  158.                   Write-Output $Output | Select-Object $PropertySet
  159.                }
  160.             }
  161.          }
  162.       }
  163.    }
  164. }
  165. }
  166.  
  167. # This is nested stuff, so that you can call the SCRIPT, and after it defines the global function, we will call that.
  168. Get-Parameter @PSBoundParameters

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me