Christoph Caesar Christoph Caesar - 3 months ago 11
PowerShell Question

Powershell pipelines null values to own function

Disclaimer: I started from zero with Powershell (okay, I had some .NET knowledge from C# and I used the Linux and Windows command lines before) only two days ago so my mistake may be obvious, but I searched for three hours with this problem and came up with nothing.

My script should do something with every .pdf file (what exactly doesn't matter). For this, I have written a function that gets called like this:

Get-ChildItem -Recurse -Filter *.pdf | foreach { $_.FullName} | ProcessPdfCreator


I tried to rework the foreach-loop that my function gets called in the braces, but that doesn't change anything. Anway my script looks like this:

Function ProcessPdfCreator {
[cmdletbinding()]
Param (
[parameter(ValueFromPipeline=$True)]
[string]$filepath
)
Begin{
}
Process{
#Do something with $filepath
}

End {}
}


My Tests so far have resulted that the $filepath variable always is null; but I used
echo $_.FullName
to cofirm that the name is correct before the piping/function calling.

Thanks in advance for any help,

Christoph

PS: I am forced to work with Powershell 2.0 on this PC, but as far as I can tell this code should work here nonetheless.

Answer
Get-ChildItem -Recurse -Filter *.pdf | select -ExpandProperty FullName | ProcessPdfCreator

Additionaly, remove casting to [string]

Function ProcessPdfCreator {
[cmdletbinding()]
Param (
    [parameter(ValueFromPipeline=$True)]
    $filepath
)
Begin{
}
Process{
    #Do something with $filepath
}

End {}
}