PoshCode Logo PowerShell Code Repository

Get-Parameter by halr9000 7 years ago (modification of post by halr9000 view diff)
View followups from halr9000 | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/549"></script>download | new post

Get-Parameter is used to obtain all of the parameters for a cmdlet. It also returns info like aliases and whether a parameter is mandatory.

Usage:

Get-Parameter <Cmdlet Name>

Revisions:

  1. function Get-Parameter ( $Cmdlet, [switch]$ShowCommon, [switch]$Full ) {
  2.  
  3.         $command = Get-Command $Cmdlet -ea silentlycontinue
  4.  
  5.         # resolve aliases (an alias can point to another alias)
  6.         while ($command.CommandType -eq "Alias") {
  7.                 $command = Get-Command ($command.definition)
  8.         }
  9.         if (-not $command) { return }
  10.  
  11.         foreach ($paramset in $command.ParameterSets){
  12.                 $Output = @()
  13.                 foreach ($param in $paramset.Parameters) {
  14.                         if ( ! $ShowCommon ) {
  15.                                 if ($param.aliases -match "vb|db|ea|wa|ev|wv|ov|ob|wi|cf") { continue }
  16.                         }
  17.                         $process = "" | Select-Object Name, Type, ParameterSet, Aliases, Position, IsMandatory,
  18.                         Pipeline, PipelineByPropertyName
  19.                         $process.Name = $param.Name
  20.                         if ( $param.ParameterType.Name -eq "SwitchParameter" ) {
  21.                                 $process.Type = "Boolean"
  22.                         }
  23.                         else {
  24.                                 switch -regex ( $param.ParameterType ) {
  25.                                         "Nullable``1\[(.+)\]" { $process.Type = $matches[1].Split('.')[-1] + " (nullable)" ; break }
  26.                                         default { $process.Type = $param.ParameterType.Name }
  27.                                 }
  28.                         }
  29.                         if ( $paramset.name -eq "__AllParameterSets" ) { $process.ParameterSet = "Default" }
  30.                         else { $process.ParameterSet = $paramset.Name }
  31.                         $process.Aliases = $param.aliases
  32.                         if ( $param.Position -lt 0 ) { $process.Position = $null }
  33.                         else { $process.Position = $param.Position }
  34.                         $process.IsMandatory = $param.IsMandatory
  35.                         $process.Pipeline = $param.ValueFromPipeline
  36.                         $process.PipelineByPropertyName = $param.ValueFromPipelineByPropertyName
  37.                         $output += $process
  38.                 }
  39.                 if ( ! $Full ) {
  40.                         $Output | Select-Object Name, Type, ParameterSet, IsMandatory, Pipeline
  41.                 }
  42.                 else { Write-Output $Output }
  43.         }
  44. }

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