#requires -version 2.0 # Jeffery Hicks # http://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # # # "Those who forget to script are doomed to repeat their work." # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** Function Get-Parameter { <# .Synopsis Retrieve command parameter information. .Description Using Get-Command, this function will return information about parameters for any loaded cmdlet or function. The common parameters like Verbose and ErrorAction are omitted. Get-Parameter returns a custom object with the most useful information an administrator might need to know. Here is an example: Position : 0 Name : Name Type : System.String[] Aliases : {ServiceName} ValueFromPipeline : True Mandatory : False ParameterSet : Default .Parameter Command The name of a cmdlet or function. The parameter has an alias of Name. .Example PS C:\> get-parameter get-service Return parameter information for get-service .Example PS C:\Scripts> get-parameter mkdir | select Name,type Found 6 specific parameters for mkdir Name Type ---- ---- Path System.String[] Name System.String Value System.Object Force System.Management.Automation.SwitchParameter Credential System.Management.Automation.PSCredential UseTransaction System.Management.Automation.SwitchParameter .Example PS C:\Scripts> get-parameter get-wmiobject | sort parameterset | format-table -GroupBy ParameterSet -Property Name,Alias,Position,Type -autosize Found 18 non-common parameters for get-wmiobject ParameterSet: __AllParameterSets Name Alias Position Type ---- ----- -------- ---- ThrottleLimit System.Int32 Amended System.Management.Automation.SwitchParameter AsJob System.Management.Automation.SwitchParameter ParameterSet: class Name Alias Position Type ---- ----- -------- ---- Locale System.String Impersonation System.Management.ImpersonationLevel ParameterSet: list Name Alias Position Type ---- ----- -------- ---- Namespace System.String ComputerName System.String[] Authority System.String List System.Management.Automation.SwitchParameter Recurse System.Management.Automation.SwitchParameter ParameterSet: query Name Alias Position Type ---- ----- -------- ---- Filter System.String Property 1 System.String[] Class 0 System.String EnableAllPrivileges System.Management.Automation.SwitchParameter DirectRead System.Management.Automation.SwitchParameter ParameterSet: WQLQuery Name Alias Position Type ---- ----- -------- ---- Query System.String Credential System.Management.Automation.PSCredential Authentication System.Management.AuthenticationLevel .Inputs [string] .Outputs custom object .Link http://jdhitsolutions.com/blog/2010/07/get-parameter .Link Get-Command .Notes NAME: Get-Parameter VERSION: 1.2 AUTHOR: Jeffery Hicks LASTEDIT: July 20, 2010 Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) #> Param( [Parameter(Position=0,Mandatory=$True, ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True, HelpMessage="Enter a cmdlet name")] [ValidateNotNullorEmpty()] [Alias("name")] [string]$command ) Process { #define the set of common parameters to exclude $common=@("Verbose", "Debug", "ErrorAction", "ErrorVariable", "WarningAction", "WarningVariable", "OutVariable", "OutBuffer", "WhatIf", "Confirm") Try { $data=(Get-Command -Name $command -errorAction "Stop").parameters } Catch { Write-Warning "Failed to find command $command" } #keep going if parameters were found if ($data.count -gt 0) { #$data is a hash table $params=$data.keys | where {$common -notcontains $_} $count=($params | measure-object).count #only keep going if non-common parameters were found write-host "Found $count non-common parameters for $command" ` -ForegroundColor Green if ($count -gt 0) { #get information from each parameter $params | foreach { $name=$_ $type=$data.item($name).ParameterType $aliases=$data.item($name).Aliases $attributes=$data.item($name).Attributes if ($attributes[0].position -ge 0) { $position=$attributes[0].position } else { $position=$null } #write a custom object to the pipeline New-Object -TypeName PSObject -Property @{ Name=$name Aliases=$aliases Mandatory=$attributes[0].mandatory Position=$position ValueFromPipeline=$attributes[0].ValueFromPipeline Type=$type ParameterSet=$attributes[0].ParameterSetName } } #foreach } #if $count } #if $data else { Write-Host "$command has no defined parameters" -ForegroundColor Red } } #process } #end function #uncomment if you also want to define the alias #Set-Alias -Name gpa -Value Get-Parameter