Skip to content

Instantly share code, notes, and snippets.

@trackd
Last active May 29, 2024 20:24
Show Gist options
  • Save trackd/fbed87ed5f9975d6c993ac0796f65708 to your computer and use it in GitHub Desktop.
Save trackd/fbed87ed5f9975d6c993ac0796f65708 to your computer and use it in GitHub Desktop.
function Get-BoundParameters {
<#
.DESCRIPTION
Get-BoundParameters is a helper function that returns a hashtable of the bound parameters of the calling function.
difference between $PSBoundParameters is that it gets default value from param block as well.
.PARAMETER IncludeCommon
include the common parameters
.PARAMETER IncludeFrame
include all variables in the calling scope.
#>
[OutputType([hashtable])]
[CmdletBinding()]
param(
[Switch] $IncludeCommon,
[Switch] $IncludeFrame
)
$Callstack = (Get-PSCallStack)[1]
$FrameVariables = $Callstack.GetFrameVariables()
if ($IncludeFrame) {
return [hashtable]::new($FrameVariables)
}
$MyPSBoundParameters = @{}
$Parameters = $Callstack.InvocationInfo.MyCommand.Parameters
if ($Parameters.Count -eq 0) {
return $MyPSBoundParameters
}
$BoundParameters = $Callstack.InvocationInfo.BoundParameters
$Parameters.GetEnumerator() | ForEach-Object {
if ($IncludeCommon -And $BoundParameters.ContainsKey($_.key)) {
$MyPSBoundParameters[$_.Key] = $BoundParameters[$_.Key]
return
}
if ($FrameVariables.ContainsKey($_.Key)) {
$MyPSBoundParameters[$_.Key] = $FrameVariables[$_.Key].Value
}
}
$MyPSBoundParameters
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment