Created
March 31, 2025 23:59
-
-
Save romero126/12bcfc894e73571d02648fb914dfd981 to your computer and use it in GitHub Desktop.
Get-PSBoundParameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-PSBoundParameters { | |
<# | |
.SYNOPSIS | |
Get-PSBoundParameters | |
.DESCRIPTION | |
Get the bound parameters of the current command | |
.PARAMETER ParameterSetName | |
Filter the bound parameters by the specified ParameterSetName | |
.EXAMPLE | |
Get-PSBoundParameters | ft -a | |
> Key Value | |
> --- ----- | |
> ParamA ValueA | |
> ParamB ValueB | |
> ParamC ValueC | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter()] | |
[System.String] $ParameterSetName | |
) | |
$callstack = (Get-PSCallStack)[1] | |
$frameVariables = $Callstack.GetFrameVariables() | |
$myPSBoundParameters = [System.Collections.Generic.Dictionary[System.String, System.Object]]::new() | |
$parameters = $Callstack.InvocationInfo.MyCommand.Parameters | |
if ($Parameters.Count -eq 0) { | |
return $MyPSBoundParameters | |
} | |
$boundParameters = $callstack.InvocationInfo.BoundParameters | |
$parameters.GetEnumerator() | ForEach-Object ` | |
{ | |
# Filter by ParameterSetName if specified | |
if ($ParameterSetName -and $_.Value.Attributes.ParameterSetName -notmatch $ParameterSetName) { | |
return | |
} | |
# If the parameter is bound then use that value | |
if ($boundParameters.ContainsKey($_.Key)) { | |
$myPSBoundParameters[$_.Key] = $boundParameters[$_.Key] | |
return | |
} | |
# If the parameter is not bound then check if it is a variable in the current frame, and use that value | |
if ($frameVariables.ContainsKey($_.Key) -and $null -ne $frameVariables[$_.Key].Value) { | |
$myPSBoundParameters[$_.Key] = $frameVariables[$_.Key].Value | |
} | |
} | |
$myPSBoundParameters | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment