PoshCode Logo PowerShell Code Repository

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

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