Created
January 6, 2025 20:31
-
-
Save steviecoaster/99a59c9345324e2d319bd2933116d11b to your computer and use it in GitHub Desktop.
Get all parameters to function, including default values
This file contains 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-AllBoundParameters { | |
[CmdletBinding()] | |
Param( | |
# A simple string parameter that is mandatory | |
[Parameter(Mandatory)] | |
[String] | |
$Name, | |
# A simple switch | |
[Parameter()] | |
[Switch] | |
$Switch, | |
# A parameter with a default value | |
[Parameter()] | |
[String] | |
$DefaultValue = 'A default' | |
) | |
end { | |
$params = $PSCmdlet.MyInvocation.BoundParameters | |
$PSCmdlet.MyInvocation.MyCommand.Parameters.Keys | ForEach-Object { | |
if (-not $params.ContainsKey($_)) { | |
$params[$_] = (Get-Variable -Name $_ -Scope 0 -ErrorAction SilentlyContinue).Value | |
} | |
} | |
$params | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment