Export/Private/Get-FunctionParameters.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function Global:Get-FunctionParameters {
    <#
    .SYNOPSIS
    This is a function that will create a HashTable of all Parameters from another function-call
     
    .EXAMPLE
    PS> Get-FunctionParameters $MyInvocation
     
    .PARAMETER MyPreviousInvocation
    A mandatory parameter representing the PS builtin variable $MyInvocation
     
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        $MyPreviousInvocation
    )
    try {
        $ht = @{ }
        $ParameterList = $MyPreviousInvocation.MyCommand.Parameters
        foreach ($key in $ParameterList.keys) {
            $var = Get-Variable -Name $key -ErrorAction SilentlyContinue;
            if ($var) {
                $ht[$var.Name] = $var.Value
            }
        }
        $ht
    }
    catch {
        Write-Error -Message $_.Exception.Message
    }
}