I am trying to find a way to get all the parameter information from a powershell script. Ex script:
function test() { Param( [string]$foo, [string]$bar, [string]$baz = "baz" ) foreach ($key in $MyInvocation.BoundParameters.keys) { write-host "Parameter: $($key) -> $($MyInvocation.BoundParameters[$key])" } } test -foo "foo!"
I would like to get the values โโof $bar and $baz dynamically without knowing the parameter names ahead of time.
I looked at the properties and methods of $MyInvocation , but I see nothing but the parameters that are set / passed.
Update 1:
I am close to getting it:
function test() { Param( [string]$foo, [string]$bar, [string]$baz = "baz" ) foreach($var in (get-variable -scope private)) { write-host "$($var.name) -> $($var.value)" } } test -foo "foo!"
If I could filter out the script options and default options, I would be fine to go.
Update 2: The final working solution is as follows:
function test { param ( [string] $Bar = 'test' , [string] $Baz , [string] $Asdf ) $ParameterList = (Get-Command -Name $MyInvocation.InvocationName).Parameters; foreach ($key in $ParameterList.keys) { $var = Get-Variable -Name $key -ErrorAction SilentlyContinue; if($var) { write-host "$($var.name) > $($var.value)" } } } test -asdf blah;
parameters powershell
Eric Longstreet
source share