Powershell管道将值赋值为自己的函数

时间:2016-08-12 09:24:13

标签: powershell powershell-v2.0

我的脚本应该对每个.pdf文件做一些事情(究竟什么都不重要)。为此,我写了一个像这样被调用的函数:

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

我尝试重做我的函数在大括号中调用的foreach循环,但这并没有改变任何东西。 Anway我的脚本看起来像这样:

Function ProcessPdfCreator {
    [cmdletbinding()]
    Param (
        [parameter(ValueFromPipeline=$true)]
        [string]$filepath
    )

    Begin{}
    Process{
        #Do something with $filepath
    }
    End {}
}

到目前为止,我的测试结果是$filepath变量始终为空;但我使用echo $_.FullName确认在管道/函数调用之前名称是正确的。

PS:我被迫在这台PC上使用 PowerShell 2.0 ,但据我所知,这段代码应该可以在这里使用。

1 个答案:

答案 0 :(得分:0)

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

另外,删除转换为[string]

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

End {}
}