This surprised me. When a variable is set via pipeline, the begin block doesn’t ‘see’ it? If I set the variable via a parameter argument, the begin block sees it. I was looking for a way to determine if the variable was set, but not sure how i can do that if I allow pipeline input.
scripts:\ PS > . .\functest-pipeline.ps1 scripts:\ PS > test-pipeline -list "asdf" -ver VERBOSE: [begin]: start asdf VERBOSE: asdf VERBOSE: [process]: start asdf VERBOSE: [process]: asdf VERBOSE: process block foreach item: VERBOSE: end block scripts:\ PS > "asdf" | test-pipeline -ver VERBOSE: [begin]: start VERBOSE: VERBOSE: no list VERBOSE: [process]: start asdf VERBOSE: [process]: asdf VERBOSE: process block foreach item: VERBOSE: end block scripts:\
function test-pipeline {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 0)]
#[ValidateNotNullOrEmpty()]
[alias("name")]
[string[]]
$list )
begin {
write-verbose "[begin]: begin"
"$list"
write-verbose "$list"
if (!($list)) {
write-verbose "no list"
}
}
process {
write-verbose "[process]: begin"
"$list"
write-verbose "[process]: $list"
foreach ($item in $list) {
write-verbose "process block foreach item: $vm"
}
}
end {
write-verbose "end block"
$results
}
}