When does Powershell honour default values when using $null splat parameters? -
consider following function:
function f1{ param( $sb = {}, $s = '' ) if ($sb -isnot [scriptblock]) { 'scriptblock' } if ($s -isnot [string] ) { 'string' } } now invoke splat parameter:
ps c:\> $splat = @{foo='bar'} ps c:\> f1 @splat as expected, nothing returned. try again $null splat parameter:
ps c:\> $splat = $null ps c:\> f1 @splat scriptblock oddly, scriptblock returned. clearly, @ least [scriptblock] parameter, powershell not honoring default value when $null splat parameter used. powershell honor default value [string]. going on here?
for types powershell honour default values when using $null splat parameters?
here's demonstration understand what's happening
$splat = @{foo='bar'} "$(&{$args}@splat)" -foo: bar when splat hash table, gets converted -key: value string pairs become parameters function.
now try:
$splat = $null "$(&{$args}@splat)" nothing returned. there no keys generate parameter string from, end result same not passing parameters @ all.
Comments
Post a Comment