我正在尝试编写一个 powershell 函数,该函数通过管道从 get-content commandlet 接收文件列表并处理它们。管道看起来像这样:
get-content D:\filelist.txt | test-pipeline
Run Code Online (Sandbox Code Playgroud)
为了简单起见,下面的函数应该只显示文本文件的每一行。
function test-pipeline
{
<#
.Synopsis
#>
[CmdletBinding( SupportsShouldProcess=$true)]
Param([Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[array]$filelist
)
foreach ($item in $filelist)
{
$item
}
}
Run Code Online (Sandbox Code Playgroud)
我的文件列表是一个普通的 .txt 文件,如下所示。
line 1
line 2
line 3
line 4
line 5
Run Code Online (Sandbox Code Playgroud)
无论我向函数传递什么类型的参数,它都不会起作用,并且仅在 $filelist 变量中显示文本文件的最后一行。有人可以帮忙吗?Powershell 版本是 v2 提前致谢