命令支持管道

来源:互联网 发布:淘宝店加权重有哪些 编辑:程序博客网 时间:2024/06/06 14:15

命令支持管道

支持管道,需要为参数加
[Parameter(ValueFromPipeline)]
特性。

Function Test-Something {    [Alias("ts")]    [CmdletBinding(SupportsShouldProcess)]    param(    [Parameter(ValueFromPipeline)]    $What)    begin{    }    Process {        if ($pscmdlet.ShouldProcess($What)) {            foreach ($item in $what)            {                dir $item            }         }    } # End Process    end {    }} # End如果命令支持数组,比如 dir,就可把             foreach ($item in $What)            {                dir $item            } 换成dir $What

调用
ts c:, d:\
可以使用管道:
“c:\”, “d:\” | ts

使用管道时,必须加引号。

如果是不支持数组的命令,比如, Write-Host,就要注意了,这时的输出结果就不一样了。
Write-Host “Output $What”
ts c:, d:\
Output c:\ d:\
这并不是我们想要的结果。

“c:\”, “d:\” | ts
Output c:\
Output d:\

0 0
原创粉丝点击