Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 28f8cbb

Browse files
committedAug 20, 2016
small fix
1 parent 113da81 commit 28f8cbb

File tree

2 files changed

+21
-81
lines changed

2 files changed

+21
-81
lines changed
 

‎PowerShell Internals/Get-FunctionDefaultParameter

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,40 @@
1-
#Requires -Version 4
2-
31
function Get-FunctionDefaultParameter
42
{
53
<#
64
.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.
136
147
.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
259
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.
2812
29-
.OUTPUTS
30-
System.HashTable
3113
#>
3214
[CmdletBinding()]
3315
[OutputType([hashtable])]
3416
param
3517
(
3618
[Parameter(Mandatory)]
3719
[ValidateNotNullOrEmpty()]
38-
[string]$Name
20+
[string]$FunctionName
3921
)
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
4937
{
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
5839
}
59-
$ht
6040
}

0 commit comments

Comments
 (0)
Please sign in to comment.