1
- # Requires -Version 4
2
-
3
1
function Get-FunctionDefaultParameter
4
2
{
5
3
<#
6
4
. SYNOPSIS
7
- This function is used to fine all default parameter values defined in a function. In order for this to work, be sure the
8
- function you're specifying is either in a module availabe to be auto-imported or the function has manually been loaded into
9
- the session.
10
-
11
- This function will enumerate all default values in a function and output their values. If it sees a value that's an expression,
12
- it will expand the expression and output the result rather than just the string representation.
5
+ This is a function that will find all of the default parameter names and values from a given function.
13
6
14
7
. EXAMPLE
15
- PS> function MyFunction { param($Param1 = 'Default1',$Param2 = 'Default2') }
16
- PS> Get-FunctionDefaultParameter -Name MyFunction
17
-
18
- Name Value
19
- ---- -----
20
- Param1 Default1
21
- Param2 Default2
22
-
23
- . PARAMETER Name
24
- The name of the function loaded into the session.
8
+ PS> Get-FunctionDefaultParameter -FunctionName Get-Something
25
9
26
- . INPUTS
27
- None. You cannot pipe objects to function- name.
10
+ . PARAMETER FuntionName
11
+ A mandatory string parameter representing the name of the function to find default parameters to .
28
12
29
- . OUTPUTS
30
- System.HashTable
31
13
#>
32
14
[CmdletBinding ()]
33
15
[OutputType ([hashtable ])]
34
16
param
35
17
(
36
18
[Parameter (Mandatory )]
37
19
[ValidateNotNullOrEmpty ()]
38
- [string ]$Name
20
+ [string ]$FunctionName
39
21
)
40
-
41
- $ast = (Get-Command $Name ).ScriptBlock.Ast
42
-
43
- $select = @ { n = ' Name' ; e = { $_.Name.VariablePath.UserPath } },
44
- @ { n = ' Value' ; e = { $_.DefaultValue.Extent.Text } }
45
-
46
- $params = $ast.FindAll ({ $args [0 ] -is [System.Management.Automation.Language.ParameterAst ] }, $true ) | where { $_.DefaultValue } | select $select
47
- $ht = @ { }
48
- foreach ($param in $params )
22
+ try
23
+ {
24
+ $ast = (Get-Command $FunctionName ).ScriptBlock.Ast
25
+
26
+ $select = @ { n = ' Name' ; e = { $_.Name.VariablePath.UserPath } },
27
+ @ { n = ' Value' ; e = { $_.DefaultValue.Extent.Text -replace " `" |'" } }
28
+
29
+ $ht = @ {}
30
+ @ ($ast.FindAll ({ $args [0 ] -is [System.Management.Automation.Language.ParameterAst ] }, $true ) | Where-Object { $_.DefaultValue } | Select-Object $select ).foreach ({
31
+ $ht [$_.Name ] = $_.Value
32
+ })
33
+ $ht
34
+
35
+ }
36
+ catch
49
37
{
50
- if ($param.Value -match ' \(.*\)' )
51
- {
52
- $ht [$param.Name ] = Invoke-Expression $param.Value
53
- }
54
- else
55
- {
56
- $ht [$param.Name ] = $param.Value -replace " '|`"`" "
57
- }
38
+ Write-Error - Message $_.Exception.Message
58
39
}
59
- $ht
60
40
}
0 commit comments