I ran into a weird issue that I do not know how to explain. I hope someone could shed some light for me.
I would like to create a function which takes a parameter of an array, let's say $scriptnames and users have an option to pipe it from previous results should they prefer. I stripped away the unrelated stuff to better illustrate my confusion.
In the process of the module, I read each item from the array and just print the item.
My function:
function Get-Test
{
[CmdletBinding()]
param
(
[parameter(mandatory=$true,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$true)]
[string[]]$scriptNames
)
BEGIN
{
}
PROCESS
{
foreach ($scriptName in $scriptNames)
{
Write-Verbose "Executing: $scriptname"
}
}
END{}
Here was what confused me:
Scenario 1:
I used this command to get the list of the file in my directory:
get-childitem | Select-Object {$_.BaseName}
The list of file was return correctly without extension:
However, when I piped the results to my function, I got this print out:
Notice the unwanted $_.BaseName= literal was added.
Scenario 2:
However, if I issue this command:
get-childitem | Select-Object $_.BaseName
The results did not really filter out the basename only
but when piping to my function:
get-childitem | Select-Object $_.BaseName|
Get-Test -Verbose
the $_basename literal was not included:
However, the basename included the extension, which really confused me.
Can someone see something flaring up at you? and why did the following things happen:
1) Why was $_.BaseName literal tagged in the print out in scenario 1, which I did not ask for?
2) Why the select did not seem to work in scenario 2, but the print-out did not have $_.baseName with the exact same script?
3) What do I need to correct in my script to print out the filename only without the extension and without the literal $_.BaseName?




Select-Object {$_.BaseName}->ForEach-Object {$_.BaseName}