Skip to content

Instantly share code, notes, and snippets.

@trackd
Created May 30, 2024 13:12
Show Gist options
  • Save trackd/1d146518ead9d0cf7641e9174d3006b1 to your computer and use it in GitHub Desktop.
Save trackd/1d146518ead9d0cf7641e9174d3006b1 to your computer and use it in GitHub Desktop.
function Get-InvocationStatement {
[cmdletbinding()]
param(
[object] $Statement = $MyInvocation.Statement
)
$tokens = $null
$psb = [ordered]@{}
$null = [System.Management.Automation.Language.Parser]::ParseInput($Statement, [ref] $tokens, [ref] $null)
$filteredTokens = $tokens | Where-Object { $_.Kind -in @('Variable','SplattedVariable','Parameter') }
for ($i = 0; $i -lt $filteredTokens.Count; $i++) {
$token = $filteredTokens[$i]
if ($token.Kind -eq 'Parameter') {
$psb[$token.ParameterName] = [ordered]@{
Value = $ExecutionContext.SessionState.PSVariable.GetValue($token.ParameterName)
Type = $token.Kind
}
}
if ($token.Kind -eq 'Variable') {
$Parametername = $filteredTokens[$i - 1].ParameterName
$psb[$Parametername].VariablePath = $token.VariablePath
}
if ($token.Kind -eq 'SplattedVariable') {
$CurrentToken = $token
$ExecutionContext.SessionState.PSVariable.GetValue($token.Name).GetEnumerator() | ForEach-Object {
$psb[$_.Name] = [ordered]@{
Value = $_.Value
Type = $CurrentToken.Kind
VariablePath = $CurrentToken.VariablePath
}
}
}
}
$psb
}
function test {
param(
[int] $param1,
[int] $param2,
[int] $param3,
[switch] $param4,
[String] $paramX = 'default value'
)
Get-InvocationStatement $MyInvocation.Statement
}
$foo = 3
$splat = @{
param1 = 1
}
test @splat -param2 2 -param3 $foo -Param4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment