Powershell advanced functions: are optional parameters supposed to get initialized?
- by Richard Berg
filter CountFilter($StartAt = 0)
{
Write-Output ($StartAt++)
}
function CountFunction
{
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true, Mandatory=$true)]
$InputObject,
[Parameter(Position=0)]
$StartAt = 0
)
process
{
Write-Output ($StartAt++)
}
}
$fiveThings = $dir | select -first 5 # or whatever
"Ok"
$fiveThings | CountFilter 0
"Ok"
$fiveThings | CountFilter
"Ok"
$fiveThings | CountFunction 0
"BUGBUG ??"
$fiveThings | CountFunction
I searched Connect and didn't find any known bugs that would cause this discrepancy. Anyone know if it's by design?