Powershell 将空值通过管道传递给自己的函数
Powershell pipelines null values to own function
我的脚本应该对每个 .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
变量始终为空;但我在 piping/function 调用之前使用 echo $_.FullName
确认名称是正确的。
PS:我被迫在这台 PC 上使用 PowerShell 2.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 {}
}
我的脚本应该对每个 .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
变量始终为空;但我在 piping/function 调用之前使用 echo $_.FullName
确认名称是正确的。
PS:我被迫在这台 PC 上使用 PowerShell 2.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 {}
}