PoshCode Archive  Artifact [ccd2eb0fa8]

Artifact ccd2eb0fa8f52722a80b6c8f04253da18479dae13004838545cc10d6085b45b4:

  • File Get-Parameter.ps1 — part of check-in [cd42a4c5e8] at 2018-06-10 13:55:51 on branch trunk — 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. (user: halr9000 size: 3151)

# encoding: ascii
# api: powershell
# title: Get-Parameter
# description: 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.
# version: 0.1
# type: function
# author: halr9000
# license: CC0
# function: Get-Parameter
# x-poshcode-id: 548
# x-derived-from-id: 549
# x-archived: 2016-06-14T16:37:31
# x-published: 2009-08-22T22:28:00
#
# Usage:
# Get-Parameter <Cmdlet Name>
# Revisions:
# 0.8 (halr9000)
# Does not show common params by default, added switch to override
# Added Position property
# 0.81 (halr9000)
# Added Type property
# Added WhatIf and Confirm to list of common params
# 0.90 (halr9000)
# Added Type (cool!) and two Pipeline properties
# Added Full switch, if enabled you get it all, otherwise…
# Pruned default output so that it fits better into a narrow table.
# Special one time bonus offer: Now includes “gpm” helper function!  This is just a sample scenario which I find useful.
# 0.91 (oisin/x0n)
# Added alias resolving (included nested aliases)
# If command not found, no longer returns a single empty parameter definition
# 0.92 (halr9000)
# Tweaked output of types to handle some ugly nullable generics and SwitchParameter now says Boolean which is more descriptive
#
function Get-Parameter ( $Cmdlet, [switch]$ShowCommon, [switch]$Full ) {
	
	$command = Get-Command $Cmdlet -ea silentlycontinue	

	# resolve aliases (an alias can point to another alias)
	while ($command.CommandType -eq "Alias") {
		$command = Get-Command ($command.definition)
	}
	if (-not $command) { return }
	
	foreach ($paramset in $command.ParameterSets){
		$Output = @()
		foreach ($param in $paramset.Parameters) {
			if ( !$ShowCommon ) {
				if ($param.aliases -match "vb|db|ea|wa|ev|wv|ov|ob|wi|cf") { continue }
			}
			$process = "" | Select-Object Name, Type, ParameterSet, Aliases, Position, IsMandatory,
				Pipeline, PipelineByPropertyName
			$process.Name = $param.Name
			if ( $param.ParameterType.Name -eq "SwitchParameter" ) {
				$process.Type = "Boolean"
			}
			else {
				switch -regex ( $param.ParameterType ) {
					"Nullable``1\[System.Int32\]"		{ $process.Type = "Int32 (nullable)"; break }
					"Nullable``1\[System.Boolean\]" 	{ $process.Type = "Boolean (nullable)"; break }
					"Nullable``1\[(.+)\]"		{ $process.Type = $matches[1].Split('.')[-1]; break }
					default			{ $process.Type = $param.ParameterType.Name }
				}
			}
			if ( $paramset.name -eq "__AllParameterSets" ) { $process.ParameterSet = "Default" }
			else { $process.ParameterSet = $paramset.Name }
			$process.Aliases = $param.aliases
			if ( $param.Position -lt 0 ) { $process.Position = $null }
			else { $process.Position = $param.Position }
			$process.IsMandatory = $param.IsMandatory
			$process.Pipeline =  $param.ValueFromPipeline
			$process.PipelineByPropertyName = $param.ValueFromPipelineByPropertyName
			$output += $process
		}
		if ( !$Full ) { 
			$Output | Select-Object Name, Type, ParameterSet, IsMandatory, Pipeline
		}
		else { Write-Output $Output }
	}
}