Private/NestedFunctions/Out-DebugParameter.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
37
38
39
40
41
42
<#
.Synopsis
    Helper function to format debug parameter output.
.Example
    $PSBoundParameters | Out-DebugParameter | Write-Debug
#>

function Out-DebugParameter
{
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [AllowEmptyCollection()]
        $InputObject
    )
    Begin
    {
        $CommonParameters = ([System.Management.Automation.PSCmdlet]::CommonParameters, [System.Management.Automation.PSCmdlet]::OptionalCommonParameters)
    }
    Process
    {
        $MyName = $MyInvocation.MyCommand.Name
        $CallStack = Get-PSCallStack
        $ParentScript = $CallStack[$CallStack.Command.IndexOf($MyName) + 1]
        $ParentParentScript = $CallStack[$CallStack.Command.IndexOf($MyName) + 2]
        # Write-Debug (')
        $Output = $InputObject.GetEnumerator() | Where-Object {
            $CommonParameters -notcontains $_.Key
        } | Select-Object -Property:(
            @{
                Name       = 'Parameter'
                Expression = { $_.Key }
            },
            @{
                Name       = 'Value'
                Expression = { $_.Value }
            }
        )
        $Parameters = ($Output | ForEach-Object { "-$($_.Parameter):('$($_.Value -join ''',''')')" }) -join ' '
        Return ('[ScriptParameters]: ' + ($ParentScript.Command, $Parameters, '# Called by script/function: ' + ([System.String]$ParentParentScript.Command).Trim() + ';', 'Line: ' + ([System.String]$ParentParentScript.ScriptLineNumber).Trim()) -join ' ')
        # ' Line: ' + [System.String]$ParentScript.ScriptLineNumber
    }
}