Skip to content

Instantly share code, notes, and snippets.

@stuartsmith78
Created August 15, 2023 17:25
Show Gist options
  • Save stuartsmith78/95662858b76400f1a97d0f3d6fc0f031 to your computer and use it in GitHub Desktop.
Save stuartsmith78/95662858b76400f1a97d0f3d6fc0f031 to your computer and use it in GitHub Desktop.
Fooling around with parameters for function calls
[CmdletBinding()]
param(
[Alias("FileName")]
[string]$paramFileName,
[Alias("Identity")]
[string]$paramIdentity,
[Alias("GlipGlop")]
[switch]$paramGlipGlop=$false,
[Alias("Count")]
[int]$paramCount
)
function fv(
[scriptblock]$fn
)
{
foreach ($param in $PSBoundParameters.Keys)
{
Write-Verbose ("Starting $($MyInvocation.MyCommand)");
Write-verbose ("Key={0} Value={1}" -f $param,$PSBoundParameters[$param])
foreach ($arg in $args)
{
Write-Verbose ("passed argument: $arg")
}
}
Invoke-Command -ScriptBlock $fn #-ArgumentList @args;
}
function Do-SomeStuff
{
param(
[Alias("GOAT")]
[string]$pGOAT,
[Alias("RunnerUp")]
[string]$pRunnerUp
)
Write-Host "I did a bunch of stuff"
}
# this are the parameters from the main function
foreach ($param in $PSBoundParameters.Keys)
{
Write-Verbose ("Starting $($MyInvocation.MyCommand)");
Write-verbose ("Key={0} Value={1}" -f $param,$PSBoundParameters[$param])
foreach ($arg in $args)
{
Write-Verbose ("passed argument: $arg")
}
}
Do-SomeStuff -GOAT "Me!" -RunnerUp "You.";
if ($PSBoundParameters["Verbose"])
{
fv { Do-SomeStuff -GOAT "Donald Knuth" -RunnerUp "Dennis Ritchie and Brian Kernigan" } -Verbose;
} else {
fv { Do-SomeStuff -GOAT "Donald Knuth" -RunnerUp "Dennis Ritchie and Brian Kernigan" };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment