【发布时间】:2010-10-27 11:41:28
【问题描述】:
我正在尝试编写一个可以获取管道输入的 PowerShell 脚本(并且预计会这样做),但尝试类似
ForEach-Object {
# do something
}
从命令行使用脚本时实际上不起作用,如下所示:
1..20 | .\test.ps1
有办法吗?
注意:我知道函数和过滤器。这不是我要找的。p>
【问题讨论】:
标签: powershell
我正在尝试编写一个可以获取管道输入的 PowerShell 脚本(并且预计会这样做),但尝试类似
ForEach-Object {
# do something
}
从命令行使用脚本时实际上不起作用,如下所示:
1..20 | .\test.ps1
有办法吗?
注意:我知道函数和过滤器。这不是我要找的。p>
【问题讨论】:
标签: powershell
在 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
【讨论】:
这可行,可能还有其他方法可以做到:
foreach ($i in $input) {
$i
}
17:12:42 PS>1..20 | .\cmd-input.ps1
1
2
3
-- 剪辑 --
18
19
20
搜索“powershell $input variable”,您将找到更多信息和示例。
一对夫妇在这里:
PowerShell Functions and Filters PowerShell Pro!
(请参阅“使用 PowerShell 特殊变量“$input””一节)
“脚本、函数和脚本块都可以访问 $input 变量,该变量为传入管道中的元素提供枚举器。”
或
$input gotchas « Dmitry’s PowerBlog PowerShell and beyond
“...基本上 $input 在一个枚举器中,它提供对您拥有的管道的访问。”
对于 PS 命令行,而不是 DOS 命令行 Windows 命令处理器。
【讨论】:
ntvdm,这是 Windows NT 所拥有的 DOS VM,在 64 位系统上不再存在。 cmd.exe 不是 DOS;它是 Windows 命令处理器,除了黑底灰字之外,与 DOS 没有任何共同之处。
您可以编写一个过滤器,它是这样一个函数的特例:
filter SquareIt([int]$num) { $_ * $_ }
或者你可以像这样创建一个类似的函数:
function SquareIt([int]$num) {
Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
$_ * $_
}
End {
# Executes once after last pipeline object is processed
}
}
以上作为交互式函数定义工作,或者如果在脚本中可以点缀到您的全局会话(或其他脚本)中。但是,您的示例表明您需要一个脚本,所以这里它位于一个可直接使用的脚本中(不需要点):
--- Contents of test.ps1 ---
param([int]$num)
Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
$_ * $_
}
End {
# Executes once after last pipeline object is processed
}
在 PowerShell V2 中,这与“高级函数”有所不同,它嵌入了具有与编译的 cmdlet 相同的参数绑定功能的函数。有关差异的示例,请参阅此blog post。另请注意,在此高级功能案例中,您不使用 $_ 来访问管道对象。借助高级功能,管道对象可以像使用 cmdlet 一样绑定到参数。
【讨论】:
以下是使用管道输入的最简单的脚本/函数示例。每个行为都与管道到“echo”cmdlet 的行为相同。
作为脚本:
# 回声管.ps1 Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
echo $_
}
End {
# Executes once after last pipeline object is processed
}
# Echo-Pipe2.ps1
foreach ($i in $input) {
$i
}
作为函数:
Function Echo-Pipe {
Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
echo $_
}
End {
# Executes once after last pipeline object is processed
}
}
Function Echo-Pipe2 {
foreach ($i in $input) {
$i
}
}
例如
PS > . theFileThatContainsTheFunctions.ps1 # This includes the functions into your session
PS > echo "hello world" | Echo-Pipe
hello world
PS > cat aFileWithThreeTestLines.txt | Echo-Pipe2
The first test line
The second test line
The third test line
【讨论】: