PowerShell 技能连载 - 处理数据(第 1 部分)

这是关于 PowerShell 函数如何通过管道或参数接受数据的三个技巧中的第一个。

在第一部分中,函数实时处理输入的信息。这消耗最少的内存并且快速提供结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#requires -Version 2
function Process-Data
{
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[Object[]]
$Object )

process {
foreach ($element in $Object)
{
"Processing received element $element..."
}
}
}

请注意如何通过参数调用函数:

1
2
3
4
5
6
7
8
PS C:\> Process-Data -Object 1
Processing received element 1...

PS C:\> Process-Data -Object 1,2,3,4
Processing received element 1...
Processing received element 2...
Processing received element 3...
Processing received element 4...

您也可以通过管道传送信息:

1
2
3
4
5
PS C:\> 1..4 | Process-Data
Processing received element 1...
Processing received element 2...
Processing received element 3...
Processing received element 4...

PowerShell 技能连载 - 处理数据(第 1 部分)

http://blog.vichamp.com/2016/02/03/process-data-part-1/

作者

吴波

发布于

2016-02-03

更新于

2022-07-06

许可协议

评论