出于某种原因,我的计划是拒绝在这种情况下工作:管道向用户函数参数的问题
- 我有一个用来取代cmdlet的,由于缺乏灵活性
- 我的自定义函数我通过这个功能的文件或文件夹对象,通过管道
这是函数:
function Get-ChildItemCustom {
Param(
[Parameter(ValueFromPipeline=$true)]
[System.IO.FileSystemInfo] $item,
[Parameter(ValueFromPipeline=$false)]
[string] $archive_path
)
Process {
Get-ChildItem $item
}
}
我想能够使用这个功能就像Get-ChildItem
:输入一个[System.IO.FileSystemInfo]
对象,并获取所有的孩子(排序有一些额外的标准,我没有包括在这里)作为输出。
这是我如何调用该函数:
Get-ChildItem $parentfolder_path |
Get-ChildItemCustom |
Do-SomethingElse
错误再次解释说,Get-ChildItem
的结果(这是可验证[System.IO.FileSystemInfo]
类型)被视为字符串。
不能转换 “E:\ DATA \ VHG-ITC-TEST \新建文件夹\档案” 类型 “System.String” 的值输入 “System.IO.FileSystemInfo”。
参数之前的类型并不总是存在的。当$item
没有明确有型,功能会误读输入(据说仅服用Name
属性作为输入):
GET-ChildItem:找不到路径“C:\ WINDOWS \ SYSTEM32 \新建文件夹”因为它不存在。
所以该函数似乎无法正确接受对象输入。我要不惜一切代价避免使用字符串,只是走动的对象。我是否设置了错误的参数?我能做什么?