Check this solution out. This uses the CmdletBinding()
attribute, which provides some additional metadata through the use of the $PSCmdlet
built-in variable. You can:
- Dynamically retrieve the command’s name, using
$PSCmdlet
- Get a list of the parameter for the command, using
Get-Command
- Examine the value of each parameter, using the
Get-Variable
cmdlet
Code:
function test {
[CmdletBinding()]
param (
[string] $Bar="test"
, [string] $Baz
, [string] $Asdf
)
# Get the command name
$CommandName = $PSCmdlet.MyInvocation.InvocationName;
# Get the list of parameters for the command
$ParameterList = (Get-Command -Name $CommandName).Parameters;
# Grab each parameter value, using Get-Variable
foreach ($Parameter in $ParameterList) {
Get-Variable -Name $Parameter.Values.Name -ErrorAction SilentlyContinue;
#Get-Variable -Name $ParameterList;
}
}
test -asdf blah;
Output
The output from the command looks like this:
Name Value
---- -----
Bar test
Baz
Asdf blah