我正在尝试编写一个可以获取管道输入的PowerShell脚本(并且预计会这样做),但尝试类似
ForEach-Object {
# do something
}
从命令行使用脚本时实际上不起作用,如下所示:
1..20 | .\test.ps1
有办法吗?
注意:我了解函数和过滤器。这不是我要找的。
在 v2 中,您还可以接受管道输入(通过 propertyName 或 byValue)、添加参数别名等:
function Get-File{
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)
]
[Alias('FullName')]
[String[]]$FilePath
)
process {
foreach($path in $FilePath)
{
Write-Host "file path is: $path"
}
}
}
# test ValueFromPipelineByPropertyName
dir | Get-File
# test ValueFromPipeline (byValue)
"D:\scripts\s1.txt","D:\scripts\s2.txt" | Get-File
- or -
dir *.txt | foreach {$_.fullname} | Get-File
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)